11"""Support for Victron Energy binary sensors."""
2- from __future__ import annotations
3-
4- from contextlib import suppress
5- from typing import cast
6-
7- from homeassistant .core import HomeAssistant , HassJob
82
9- from collections .abc import Callable
10- from homeassistant .helpers .typing import StateType
3+ from __future__ import annotations
114
125from dataclasses import dataclass
6+ import logging
7+ from typing import cast
138
14- from homeassistant .helpers .update_coordinator import CoordinatorEntity
15-
9+ from homeassistant .components .binary_sensor import (
10+ DOMAIN as BINARY_SENSOR_DOMAIN ,
11+ BinarySensorEntity ,
12+ BinarySensorEntityDescription ,
13+ )
1614from homeassistant .config_entries import ConfigEntry
17- from homeassistant .core import HomeAssistant
15+ from homeassistant .core import HassJob , HomeAssistant
16+ from homeassistant .helpers import entity
1817from homeassistant .helpers .entity_platform import AddEntitiesCallback
19- from homeassistant .helpers import event , entity
20-
21- from homeassistant .components .binary_sensor import BinarySensorEntityDescription , BinarySensorEntity , DOMAIN as BINARY_SENSOR_DOMAIN
18+ from homeassistant .helpers .update_coordinator import CoordinatorEntity
2219
23- from .coordinator import victronEnergyDeviceUpdateCoordinator
2420from .base import VictronBaseEntityDescription
25- from .const import DOMAIN , CONF_ADVANCED_OPTIONS , register_info_dict , BoolReadEntityType
21+ from .const import DOMAIN , BoolReadEntityType , register_info_dict
22+ from .coordinator import victronEnergyDeviceUpdateCoordinator
2623
27- import logging
2824_LOGGER = logging .getLogger (__name__ )
2925
26+
3027async def async_setup_entry (
3128 hass : HomeAssistant ,
3229 config_entry : ConfigEntry ,
3330 async_add_entities : AddEntitiesCallback ,
3431) -> None :
3532 """Set up Victron energy binary sensor entries."""
3633 _LOGGER .debug ("attempting to setup binary sensor entities" )
37- victron_coordinator : victronEnergyDeviceUpdateCoordinator = hass .data [DOMAIN ][config_entry .entry_id ]
34+ victron_coordinator : victronEnergyDeviceUpdateCoordinator = hass .data [DOMAIN ][
35+ config_entry .entry_id
36+ ]
3837 _LOGGER .debug (victron_coordinator .processed_data ()["register_set" ])
3938 _LOGGER .debug (victron_coordinator .processed_data ()["data" ])
4039 descriptions = []
41- #TODO cleanup
40+ # TODO cleanup
4241 register_set = victron_coordinator .processed_data ()["register_set" ]
4342 for slave , registerLedger in register_set .items ():
4443 for name in registerLedger :
4544 for register_name , registerInfo in register_info_dict [name ].items ():
46- _LOGGER .debug ("unit == " + str (slave ) + " registerLedger == " + str (registerLedger ) + " registerInfo " )
45+ _LOGGER .debug (
46+ "unit == "
47+ + str (slave )
48+ + " registerLedger == "
49+ + str (registerLedger )
50+ + " registerInfo "
51+ )
4752
4853 if isinstance (registerInfo .entityType , BoolReadEntityType ):
4954 description = VictronEntityDescription (
5055 key = register_name ,
51- name = register_name .replace ('_' , ' ' ),
56+ name = register_name .replace ("_" , " " ),
5257 slave = slave ,
5358 )
5459 _LOGGER .debug ("composed description == " + str (description ))
@@ -58,28 +63,29 @@ async def async_setup_entry(
5863 entity = {}
5964 for description in descriptions :
6065 entity = description
61- entities .append (
62- VictronBinarySensor (
63- victron_coordinator ,
64- entity
65- ))
66+ entities .append (VictronBinarySensor (victron_coordinator , entity ))
6667
67- async_add_entities (
68- entities , True
69- )
68+ async_add_entities (entities , True )
7069
7170
7271@dataclass
73- class VictronEntityDescription (BinarySensorEntityDescription , VictronBaseEntityDescription ):
72+ class VictronEntityDescription (
73+ BinarySensorEntityDescription , VictronBaseEntityDescription
74+ ):
7475 """Describes victron sensor entity."""
7576
77+
7678class VictronBinarySensor (CoordinatorEntity , BinarySensorEntity ):
7779 """A binary sensor implementation for Victron energy device."""
7880
79- def __init__ (self , coordinator : victronEnergyDeviceUpdateCoordinator , description : VictronEntityDescription ) -> None :
81+ def __init__ (
82+ self ,
83+ coordinator : victronEnergyDeviceUpdateCoordinator ,
84+ description : VictronEntityDescription ,
85+ ) -> None :
8086 """Initialize the binary sensor."""
8187 self .description : VictronEntityDescription = description
82- #this needs to be changed to allow multiple of the same type
88+ # this needs to be changed to allow multiple of the same type
8389 self ._attr_device_class = description .device_class
8490 self ._attr_name = f"{ description .name } "
8591
@@ -97,7 +103,11 @@ def __init__(self, coordinator: victronEnergyDeviceUpdateCoordinator, descriptio
97103 @property
98104 def is_on (self ) -> bool :
99105 """Return True if the binary sensor is on."""
100- data = self .description .value_fn (self .coordinator .processed_data (), self .description .slave , self .description .key )
106+ data = self .description .value_fn (
107+ self .coordinator .processed_data (),
108+ self .description .slave ,
109+ self .description .key ,
110+ )
101111 return cast (bool , data )
102112
103113 @property
@@ -109,10 +119,8 @@ def available(self) -> bool:
109119 def device_info (self ) -> entity .DeviceInfo :
110120 """Return the device info."""
111121 return entity .DeviceInfo (
112- identifiers = {
113- (DOMAIN , self .unique_id .split ('_' )[0 ])
114- },
115- name = self .unique_id .split ('_' )[1 ],
116- model = self .unique_id .split ('_' )[0 ],
122+ identifiers = {(DOMAIN , self .unique_id .split ("_" )[0 ])},
123+ name = self .unique_id .split ("_" )[1 ],
124+ model = self .unique_id .split ("_" )[0 ],
117125 manufacturer = "victron" ,
118- )
126+ )
0 commit comments