-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbinary_sensor.py
56 lines (44 loc) · 1.84 KB
/
binary_sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Google Maps binary sensor."""
from __future__ import annotations
from typing import cast
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import ATTRIBUTION, DOMAIN, NAME_PREFIX
from .coordinator import GMDataUpdateCoordinator, GMIntegData
from .helpers import ConfigID
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the binary sensor platform."""
cid = cast(ConfigID, entry.entry_id)
gmi_data = cast(GMIntegData, hass.data[DOMAIN])
coordinator = gmi_data.coordinators[cid]
async_add_entities([GoogleMapsBinarySensor(coordinator)])
class GoogleMapsBinarySensor(
CoordinatorEntity[GMDataUpdateCoordinator], BinarySensorEntity
):
"""Google Maps Binary Sensor."""
_attr_attribution = ATTRIBUTION
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
def __init__(self, coordinator: GMDataUpdateCoordinator) -> None:
"""Initialize Google Maps Binary Sensor."""
super().__init__(coordinator)
entry = coordinator.config_entry
self._attr_unique_id = entry.entry_id
self._attr_name = f"{NAME_PREFIX} {entry.title} online"
self._attr_is_on = super().available
@property
def available(self) -> bool:
"""Return if entity is available."""
return True
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._attr_is_on = super().available
super()._handle_coordinator_update()