Skip to content

Commit

Permalink
feat: Add Binary Sensor platform
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Aug 12, 2024
1 parent e72e5c1 commit e099848
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
68 changes: 68 additions & 0 deletions custom_components/solarman/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from __future__ import annotations

import logging
import asyncio
import voluptuous as vol

from functools import cached_property, partial

from homeassistant.components.template.sensor import SensorTemplate
from homeassistant.components.template.sensor import TriggerSensorEntity
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.helpers.template import Template

from homeassistant.core import HomeAssistant, callback
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory, STATE_OFF, STATE_ON
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo, format_mac
from homeassistant.helpers.entity import Entity, ToggleEntity
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.components.binary_sensor import BinarySensorEntity, BinarySensorDeviceClass

from .const import *
from .common import *
from .services import *
from .entity import SolarmanEntity

_LOGGER = logging.getLogger(__name__)

_PLATFORM = get_current_file_name(__name__)

def _create_sensor(coordinator, sensor):
try:
entity = SolarmanBinarySensorEntity(coordinator, sensor)

entity.update()

return entity
except BaseException as e:
_LOGGER.error(f"Configuring {sensor} failed. [{format_exception(e)}]")
raise

async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config.options}")
coordinator = hass.data[DOMAIN][config.entry_id]

sensors = coordinator.inverter.get_sensors()

# Add entities.
#
_LOGGER.debug(f"async_setup: async_add_entities")

async_add_entities(_create_sensor(coordinator, sensor) for sensor in sensors if ("class" in sensor and sensor["class"] == _PLATFORM))

return True

async def async_unload_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config.options}")

return True

class SolarmanBinarySensorEntity(SolarmanEntity, BinarySensorEntity):
def __init__(self, coordinator, sensor):
SolarmanEntity.__init__(self, coordinator, _PLATFORM, sensor)

@property
def is_on(self) -> bool | None:
return self._attr_state != 0
2 changes: 1 addition & 1 deletion custom_components/solarman/const.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta as td

DOMAIN = "solarman"
PLATFORMS: list[str] = ["sensor", "switch", "number", "select", "time"]
PLATFORMS: list[str] = ["sensor", "binary_sensor", "switch", "number", "select", "time"]

IP_BROADCAST = "<broadcast>"
IP_ANY = "0.0.0.0"
Expand Down

0 comments on commit e099848

Please sign in to comment.