Skip to content

Commit

Permalink
Add strategy override binary_sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
PimDoos committed Jan 31, 2025
1 parent 6ed2e42 commit 9e78472
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion custom_components/sessy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

_LOGGER = logging.getLogger(__name__)

PLATFORMS: list[Platform] = [Platform.BUTTON, Platform.SENSOR, Platform.SELECT, Platform.NUMBER, Platform.SWITCH, Platform.TIME, Platform.UPDATE]
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SENSOR, Platform.SELECT, Platform.NUMBER, Platform.SWITCH, Platform.TIME, Platform.UPDATE]


async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
Expand Down
60 changes: 60 additions & 0 deletions custom_components/sessy/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""BinarySensor to read data from Sessy"""
from __future__ import annotations

from homeassistant.config_entries import ConfigEntry
from homeassistant.components.binary_sensor import BinarySensorEntity, BinarySensorDeviceClass
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory

from sessypy.const import SessyApiCommand, SessySystemState
from sessypy.devices import SessyBattery

from .const import DOMAIN, SESSY_DEVICE
from .util import (enum_to_options_list, get_cache_command, status_string_system_state)
from .sessyentity import SessyEntity

import logging
_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities):
"""Set up the Sessy binary_sensors"""

device = hass.data[DOMAIN][config_entry.entry_id][SESSY_DEVICE]
binary_sensors = []

if isinstance(device, SessyBattery):
# Power Status
try:
power_status: dict = get_cache_command(hass, config_entry, SessyApiCommand.POWER_STATUS)

if "strategy_overridden" in power_status.get("sessy", dict()):
binary_sensors.append(
SessyBinarySensor(hass, config_entry, "Strategy Override",
SessyApiCommand.POWER_STATUS, "sessy.strategy_overridden",
BinarySensorDeviceClass.RUNNING, entity_category=EntityCategory.DIAGNOSTIC)
)

except Exception as e:
_LOGGER.warning(f"Error setting up power status binary_sensors: {e}")

async_add_entities(binary_sensors)

class SessyBinarySensor(SessyEntity, BinarySensorEntity):
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry, name: str,
cache_command: SessyApiCommand, cache_key,
device_class: BinarySensorDeviceClass = None,
transform_function: function = None,
entity_category: EntityCategory = None, enabled_default: bool = True):

super().__init__(hass=hass, config_entry=config_entry, name=name,
cache_command=cache_command, cache_key=cache_key,
transform_function=transform_function)

self._attr_device_class = device_class
self._attr_entity_category = entity_category

self._attr_entity_registry_enabled_default = enabled_default

def update_from_cache(self):
self._attr_available = self.cache_value != None
self._attr_is_on = self.cache_value

0 comments on commit 9e78472

Please sign in to comment.