Skip to content

Commit

Permalink
Merge branch 'dev' into sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
basbruss authored Mar 6, 2024
2 parents 293f086 + c18e4e5 commit 1b549db
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 18 deletions.
9 changes: 5 additions & 4 deletions custom_components/adaptive_cover/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
)
from .coordinator import AdaptiveDataUpdateCoordinator

PLATFORMS = [Platform.SENSOR, Platform.SWITCH]
PLATFORMS = [Platform.SENSOR, Platform.BINARY_SENSOR]
PLATFORMS_SW = [Platform.SENSOR, Platform.SWITCH, Platform.BINARY_SENSOR]
CONF_ENTITIES = ["sun.sun"]


Expand Down Expand Up @@ -61,17 +62,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN][entry.entry_id] = coordinator

if _climate_mode:
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS_SW)
else:
await hass.config_entries.async_forward_entry_setups(entry, [Platform.SENSOR])
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

entry.async_on_unload(entry.add_update_listener(_async_update_listener))
return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS_SW)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)

Expand Down
84 changes: 84 additions & 0 deletions custom_components/adaptive_cover/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""Binary Sensor platform for the Adaptive Cover integration."""

from __future__ import annotations

from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import CONF_SENSOR_TYPE, DOMAIN
from .coordinator import AdaptiveDataUpdateCoordinator


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Adaptive Cover binary sensor platform."""
coordinator: AdaptiveDataUpdateCoordinator = hass.data[DOMAIN][
config_entry.entry_id
]

binary_sensor = AdaptiveCoverBinarySensor(
config_entry,
config_entry.entry_id,
"Sun Infront",
False,
BinarySensorDeviceClass.MOTION,
coordinator,
)
async_add_entities([binary_sensor])


class AdaptiveCoverBinarySensor(
CoordinatorEntity[AdaptiveDataUpdateCoordinator], BinarySensorEntity
):
"""representation of a Adaptive Cover binary sensor."""

_attr_has_entity_name = True
_attr_should_poll = False
_attr_translation_key = "sun_motion"

def __init__(
self,
config_entry,
unique_id: str,
binary_name: str,
state: bool,
device_class: BinarySensorDeviceClass,
coordinator: AdaptiveDataUpdateCoordinator,
) -> None:
"""Initialize the binary sensor."""
super().__init__(coordinator=coordinator)
self.type = {
"cover_blind": "Vertical",
"cover_awning": "Horizontal",
"cover_tilt": "Tilt",
}
self._name = config_entry.data["name"]
self._device_name = self.type[config_entry.data[CONF_SENSOR_TYPE]]
self._binary_name = binary_name
self._attr_unique_id = unique_id
self._state = state
self._attr_device_class = device_class
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)},
name=self._device_name,
)

@property
def name(self):
"""Name of the entity."""
return f"{self._binary_name} {self._name}"

@property
def is_on(self) -> bool:
"""Return true if the binary sensor is on."""
return self.coordinator.data.states["binary"]
1 change: 1 addition & 0 deletions custom_components/adaptive_cover/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ async def _async_update_data(self) -> AdaptiveCoverData:
"start": NormalCoverState(cover_data).cover.solar_times()[0],
"end": NormalCoverState(cover_data).cover.solar_times()[1],
"control": control_method,
"binary": NormalCoverState(cover_data).cover.valid,
},
attributes={
"default": self.config_entry.options.get(CONF_DEFAULT_HEIGHT),
Expand Down
12 changes: 12 additions & 0 deletions custom_components/adaptive_cover/icons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"entity": {
"binary_sensor": {
"sun_motion": {
"default": "mdi:weather-sunny",
"state":{
"off": "mdi:weather-sunny-off"
}
}
}
}
}
14 changes: 0 additions & 14 deletions custom_components/adaptive_cover/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@
},
"climate":{
"data":{
"temp_entity": "Binnen temperatuur entiteit",
"presence_entity": "Presence entity",
"weather_entity": "Weather entity (optional)",
"temp_low": "Low temperature threshold",
"temp_high": "High temperature threshold"
},
"data_description":{
"temp_entity": "Binnen temperatuur entiteit",
"presence_entity": "Aanwezigheid entiteit",
"weather_entity": "Weer entiteit (optioneel)",
Expand Down Expand Up @@ -171,13 +164,6 @@
},
"climate":{
"data":{
"temp_entity": "Binnen temperatuur entiteit",
"presence_entity": "Presence entity",
"weather_entity": "Weather entity (optional)",
"temp_low": "Low temperature threshold",
"temp_high": "High temperature threshold"
},
"data_description":{
"temp_entity": "Binnen temperatuur entiteit",
"presence_entity": "Aanwezigheid entiteit",
"weather_entity": "Weer entiteit (optioneel)",
Expand Down

0 comments on commit 1b549db

Please sign in to comment.