OpenGarage binary sensor (#58030)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Daniel Hjelseth Høyer 2021-10-23 22:53:49 +02:00 committed by GitHub
parent 8bfd5e4d06
commit 26f0ea4a24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 1 deletions

View file

@ -761,6 +761,7 @@ omit =
homeassistant/components/openevse/sensor.py
homeassistant/components/openexchangerates/sensor.py
homeassistant/components/opengarage/__init__.py
homeassistant/components/opengarage/binary_sensor.py
homeassistant/components/opengarage/cover.py
homeassistant/components/opengarage/entity.py
homeassistant/components/opengarage/sensor.py

View file

@ -16,7 +16,7 @@ from .const import CONF_DEVICE_KEY, DOMAIN
_LOGGER = logging.getLogger(__name__)
PLATFORMS = ["cover", "sensor"]
PLATFORMS = ["binary_sensor", "cover", "sensor"]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

View file

@ -0,0 +1,67 @@
"""Platform for the opengarage.io binary sensor component."""
from __future__ import annotations
import logging
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.core import callback
from .const import DOMAIN
from .entity import OpenGarageEntity
_LOGGER = logging.getLogger(__name__)
SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
BinarySensorEntityDescription(
key="vehicle",
),
)
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up the OpenGarage binary sensors."""
open_garage_data_coordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
[
OpenGarageBinarySensor(
open_garage_data_coordinator,
entry.unique_id,
description,
)
for description in SENSOR_TYPES
],
)
class OpenGarageBinarySensor(OpenGarageEntity, BinarySensorEntity):
"""Representation of a OpenGarage binary sensor."""
def __init__(self, open_garage_data_coordinator, device_id, description):
"""Initialize the entity."""
self._available = False
super().__init__(open_garage_data_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."""
self._attr_name = (
f'{self.coordinator.data["name"]} {self.entity_description.key}'
)
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