Files
core/homeassistant/components/iss/sensor.py
T

74 lines
2.3 KiB
Python

"""Support for iss sensor."""
import logging
from typing import Any, override
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import CONF_SHOW_ON_MAP, EntityStateAttribute
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DEFAULT_NAME, DOMAIN
from .coordinator import IssConfigEntry, IssDataUpdateCoordinator
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
entry: IssConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the sensor platform."""
coordinator = entry.runtime_data
show_on_map = entry.options.get(CONF_SHOW_ON_MAP, False)
async_add_entities([IssSensor(coordinator, entry, show_on_map)])
class IssSensor(CoordinatorEntity[IssDataUpdateCoordinator], SensorEntity):
"""Implementation of the ISS sensor."""
_attr_has_entity_name = True
_attr_name = None
def __init__(
self,
coordinator: IssDataUpdateCoordinator,
entry: IssConfigEntry,
show: bool,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self._attr_unique_id = f"{entry.entry_id}_people"
self._show_on_map = show
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, entry.entry_id)},
name=DEFAULT_NAME,
entry_type=DeviceEntryType.SERVICE,
)
@property
@override
def native_value(self) -> int:
"""Return number of people in space."""
return self.coordinator.data.number_of_people_in_space
@property
@override
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes."""
attrs: dict[str, Any] = {}
location = self.coordinator.data.current_location
if self._show_on_map:
attrs[EntityStateAttribute.LONGITUDE] = location.get("longitude")
attrs[EntityStateAttribute.LATITUDE] = location.get("latitude")
else:
attrs["long"] = location.get("longitude")
attrs["lat"] = location.get("latitude")
return attrs