Skip to content

Commit

Permalink
fix: binary sensors were added as regular sensors
Browse files Browse the repository at this point in the history
refactor: code cleanup
  • Loading branch information
odya committed Nov 3, 2023
1 parent d3e86f6 commit 39ef150
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 51 deletions.
59 changes: 59 additions & 0 deletions custom_components/ina219_ups_hat/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""INA219 UPS Hat binary_sensors"""
from __future__ import annotations
from .coordinator import INA219UpsHatCoordinator
from .entity import INA219UpsHatEntity
from homeassistant.components.binary_sensor import BinarySensorDeviceClass, BinarySensorEntity

from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType


async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None
) -> None:
"""Set up binary sensor platform."""
# We only want this platform to be set up via discovery.
if discovery_info is None:
return

coordinator = discovery_info.get("coordinator")

sensors = [
OnlineBinarySensor(coordinator),
ChargingBinarySensor(coordinator),
]

async_add_entities(sensors)


class INA219UpsHatBinarySensor(INA219UpsHatEntity, BinarySensorEntity):
"""Base binary sensor"""

def __init__(self, coordinator: INA219UpsHatCoordinator) -> None:
super().__init__(coordinator)


class OnlineBinarySensor(INA219UpsHatBinarySensor):
def __init__(self, coordinator) -> None:
super().__init__(coordinator)
self._name = "Online"
self._attr_device_class = BinarySensorDeviceClass.PLUG

@property
def is_on(self):
return self._coordinator.data["online"]


class ChargingBinarySensor(INA219UpsHatBinarySensor):
def __init__(self, coordinator) -> None:
super().__init__(coordinator)
self._name = "Charging"
self._attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING

@property
def is_on(self):
return self._coordinator.data["charging"]
17 changes: 6 additions & 11 deletions custom_components/ina219_ups_hat/entity.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@

from .const import DOMAIN
from .coordinator import INA219UpsHatCoordinator
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.device_registry import DeviceInfo


class INA219UpsHatEntity():
def __init__(self, coordinator: INA219UpsHatCoordinator) -> None:
self._coordinator = coordinator
self._device_id = self._coordinator.id_prefix
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, coordinator.id_prefix)},
name=coordinator.name_prefix,
manufacturer="Some Chinese factory",
)

@property
def name(self):
Expand All @@ -17,15 +22,5 @@ def name(self):
def unique_id(self):
return self._coordinator.id_prefix + '_' + self._name

@property
def device_info(self) -> DeviceInfo:
"""Return the device_info of the device."""
return DeviceInfo(
identifiers={(DOMAIN, self._coordinator.id_prefix)},
name=self._coordinator.name_prefix,
manufacturer="Some Chinese factory",
sw_version="0.3.0",
)

async def async_update(self):
await self._coordinator.async_request_refresh()
2 changes: 1 addition & 1 deletion custom_components/ina219_ups_hat/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "local_polling",
"issue_tracker": "https://github.com/odya/hass-ina219-ups-hat/issues",
"requirements": ["smbus2>=0.4.2","numpy>=1.23.2"],
"version": "0.3.0"
"version": "0.3.1"
}
46 changes: 7 additions & 39 deletions custom_components/ina219_ups_hat/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Details about the INA219 UPS Hat sensor"""
"""INA219 UPS Hat sensors"""

from homeassistant.components.binary_sensor import BinarySensorDeviceClass, BinarySensorEntity
from homeassistant.components.sensor.const import SensorDeviceClass, SensorStateClass
from homeassistant import core
from homeassistant.components.sensor import SensorEntity, PLATFORM_SCHEMA
Expand Down Expand Up @@ -30,6 +29,7 @@
CONF_SCAN_INTERVAL,
CONF_SMA_SAMPLES,
DEFAULT_NAME,
DOMAIN,
)


Expand Down Expand Up @@ -64,21 +64,20 @@ async def async_setup_platform(
SocSensor(coordinator),
RemainingCapacitySensor(coordinator),
RemainingTimeSensor(coordinator),
OnlineBinarySensor(coordinator),
ChargingBinarySensor(coordinator),
]
async_add_entities(sensors)

hass.helpers.discovery.load_platform('binary_sensor', DOMAIN, {
"coordinator": coordinator
}, config)

async def async_update_data(now):
await coordinator.async_request_refresh()

async_track_time_interval(hass, async_update_data,
config.get(CONF_SCAN_INTERVAL))


# SENSORS


class INA219UpsHatSensor(INA219UpsHatEntity, SensorEntity):
"""Base sensor"""

Expand Down Expand Up @@ -141,6 +140,7 @@ def __init__(self, coordinator) -> None:
super().__init__(coordinator)
self._name = "Remaining Capacity"
self._attr_native_unit_of_measurement = "mAh"
self._attr_device_class = SensorDeviceClass.ENERGY_STORAGE
self._attr_suggested_display_precision = 0

@property
Expand All @@ -160,35 +160,3 @@ def __init__(self, coordinator) -> None:
@property
def native_value(self):
return self._coordinator.data["remaining_time"]


# BINARY SENSORS


class INA219UpsHatBinarySensor(INA219UpsHatEntity, BinarySensorEntity):
"""Base binary sensor"""

def __init__(self, coordinator: INA219UpsHatCoordinator) -> None:
super().__init__(coordinator)


class OnlineBinarySensor(INA219UpsHatBinarySensor):
def __init__(self, coordinator) -> None:
super().__init__(coordinator)
self._name = "Online"
self._attr_device_class = BinarySensorDeviceClass.PLUG

@property
def is_on(self):
return self._coordinator.data["online"]


class ChargingBinarySensor(INA219UpsHatBinarySensor):
def __init__(self, coordinator) -> None:
super().__init__(coordinator)
self._name = "Charging"
self._attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING

@property
def is_on(self):
return self._coordinator.data["charging"]

0 comments on commit 39ef150

Please sign in to comment.