1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-08 17:49:37 +01:00
Files
core/homeassistant/components/opengarage/binary_sensor.py
T
epenet 7268571587 Use runtime_data in opengarage integration (#167040)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 11:33:55 +02:00

76 lines
2.1 KiB
Python

"""Platform for the opengarage.io binary sensor component."""
from __future__ import annotations
import logging
from typing import cast
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .coordinator import OpenGarageConfigEntry, OpenGarageDataUpdateCoordinator
from .entity import OpenGarageEntity
_LOGGER = logging.getLogger(__name__)
SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
BinarySensorEntityDescription(
key="vehicle",
translation_key="vehicle",
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: OpenGarageConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the OpenGarage binary sensors."""
open_garage_data_coordinator = entry.runtime_data
async_add_entities(
OpenGarageBinarySensor(
open_garage_data_coordinator,
cast(str, entry.unique_id),
description,
)
for description in SENSOR_TYPES
)
class OpenGarageBinarySensor(OpenGarageEntity, BinarySensorEntity):
"""Representation of a OpenGarage binary sensor."""
def __init__(
self,
coordinator: OpenGarageDataUpdateCoordinator,
device_id: str,
description: BinarySensorEntityDescription,
) -> None:
"""Initialize the entity."""
self._available = False
super().__init__(coordinator, device_id, description)
@property
def available(self) -> bool:
"""Return True if entity is available."""
return super().available and self._available
@callback
def _update_attr(self) -> None:
"""Handle updated data from the coordinator."""
state = self.coordinator.data.get(self.entity_description.key)
if state == 1:
self._attr_is_on = True
self._available = True
elif state == 0:
self._attr_is_on = False
self._available = True
else:
self._available = False