Beginner for homeassistant, sorry if this is a noob question:
Trying to write a custom component for crow alarm system; i understand that i needed a python API for this custom component which communicates with socket connection, so i wrote one. So far so good.
I also copied and changed “envisalink” component for that purpose and i handled the zone status on binary_sensors. However, the module has some additional binary information like battery, mains power, dialler phone line etc.
So, how can i also add these binary sensors; does binary_sensor.py support 2 different device classes, or should i handle them in sensors.py (although all system sensors are binary), or is there a way to include them in alarm_control_panel.py?
I would appreciate if you can direct me to the right direction; or at least show me a sample of binary_sensors.py with 2 different classes if this is a thing.
The crow alarm api sends zone and system statuses as follows (I can change it if it is required):
Zones:
for i in range (1, maxZones+1):
_zoneState[i] = {'status': {'open': False, 'bypass': False, 'alarm': False, 'tamper': False},
'last_fault': 0}
System status:
_systemState = {'status':{'mains': False, 'battery': False,'tamper': False, 'line': False, 'dialler': False,
'ready': True, 'fuse': False, 'zonebattery': False, 'pendantbattery': False, 'codetamper': False}}
binary_sensor.py:
from . import (
CONF_ZONENAME,
CONF_ZONETYPE,
DATA_CRW,
SIGNAL_ZONE_UPDATE,
ZONE_SCHEMA,
CrowIPModuleDevice,
)
_LOGGER = logging.getLogger(__name__)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Crow binary sensor devices."""
configured_zones = discovery_info["zones"]
devices = []
for zone_num in configured_zones:
device_config_data = ZONE_SCHEMA(configured_zones[zone_num])
device = CrowIPModuleBinarySensor(
hass,
zone_num,
device_config_data[CONF_ZONENAME],
device_config_data[CONF_ZONETYPE],
hass.data[DATA_CRW].zone_state[zone_num],
hass.data[DATA_CRW],
)
devices.append(device)
async_add_entities(devices)
class CrowIPModuleBinarySensor(CrowIPModuleDevice, BinarySensorDevice):
"""Representation of an Crow IP Module binary sensor."""
def __init__(self, hass, zone_number, zone_name, zone_type, info, controller):
"""Initialize the binary_sensor."""
self._zone_type = zone_type
self._zone_number = zone_number
_LOGGER.debug("Setting up zone: %s", zone_name)
super().__init__(zone_name, info, controller)
async def async_added_to_hass(self):
"""Register callbacks."""
async_dispatcher_connect(self.hass, SIGNAL_ZONE_UPDATE, self._update_callback)
@property
def is_on(self):
"""Return true if sensor is on."""
return self._info["status"]["open"]
@property
def device_class(self):
"""Return the class of this sensor, from DEVICE_CLASSES."""
return self._zone_type
@callback
def _update_callback(self, zone):
"""Update the zone's state, if needed."""
if zone is None or int(zone) == self._zone_number:
self.async_schedule_update_ha_state()