gluap / pyduofern-hacs

Repository managing hacs-integration for pyduofern
MIT License
27 stars 8 forks source link

Feature Request: Add Sun Sensor 9478 #29

Closed lrusdorf closed 9 months ago

lrusdorf commented 1 year ago

Hi, I have extended the binary_sensor.py to discover the sun sensor 9478 and add it as a light sensor. Working fine with mine. Feel free to include in build.

Add class Light:

from homeassistant.components.binary_sensor import ( DEVICE_CLASS_SMOKE, DEVICE_CLASS_LIGHT, PLATFORM_SCHEMA, BinarySensorEntity )

In the device detection loop the device ID is "a5":

def setup_platform(hass, config, add_entities, discovery_info=None): """Setup the Duofern binary sensors pltaform"""

# Get the Duofern stick instance
stick = hass.data["duofern"]['stick']

# Add devices
for device in stick.config['devices']:
    # _LOGGER.warning(device['id'])
    if device['id'].startswith('ab'): # Check if this device is a smoke detector
        if device['id'] in hass.data[DOMAIN]['devices'].keys(): # Check if Home Assistant already has this device
            continue
        add_entities([DuofernSmokeDetector(device['id'], device['name'], stick, hass)]) # Add new binary sensor for smoke detectors
    if device['id'].startswith('a5'): # Check if this device is a sun sensor
        if device['id'] in hass.data[DOMAIN]['devices'].keys(): # Check if Home Assistant already has this device
            continue
        add_entities([DuofernSunSensor(device['id'], device['name'], stick, hass)]) # Add new binary sensor for sun sensors

and the Class is defined as: class DuofernSunSensor(BinarySensorEntity): """Duofern sun sensor entity"""

def __init__(self, code, desc, stick, hass, channel=None):
    """Initialize the sun sensor"""

    self._code = code
    self._id = code
    self._name = desc

    if channel:
      chanNo = "{:02x}".format(channel)
      self._id += chanNo
      self._name += chanNo

    self._state = None # Holds the state (off = clear, on = smoke detected)
    self._stick = stick # Hold an instance of the Duofern stick
    self._channel = channel
    hass.data[DOMAIN]['devices'][self._id] = self # Add device to our domain

@property
def name(self):
    """Returns the name of the sun sensor"""
    return self._name

@property
def is_on(self):
    """Returns the current state of the sun sensor"""
    return self._state == "on"

@property
def device_state_attributes(self):
    """Return the attributes of the sun sensor"""
    attributes = {
    }

    return attributes
@property
def icon(self):
    """Return the icon of the sun sensor"""
    return "mdi:sun-wireless-outline"

@property
def device_class(self):
    """Return the device class sun sensor"""
    return DEVICE_CLASS_LIGHT

@property
def should_poll(self):
    """Whether this entity should be polled or uses subscriptions"""
    return True # TODO: Add config option for subscriptions over polling

@property
def unique_id(self):
    """Return the unique id of the Duofern device"""
    return self._id

def update(self):
    """Called right before is_on() to update the current state from the stick"""
    try:
        self._state = self._stick.duofern_parser.get_state(self._code, 'state', channel=self._channel)
    except KeyError:
        self._state = None
8OND007 commented 1 year ago

thx @lrusdorf @gluap: can this be integrated ? Would be great since I have a 9478 sun sensor. Can't test it at the moment since the sun itself has to solar charge the battery of the 9478, waiting for sunny days. :-D

gluap commented 1 year ago

@lrusdorf Thank you, having contributions like this is what makes it possible to enable device types I don't have in duofern-hacs.

Could you phrase this as a pull request or (if that is easier) possibly attach the python file? Pasting it into the comment has messed with the indentation.

lrusdorf commented 1 year ago

@gluap Thanks for having done all the pre-work ;-) I just had to extend your basis. Yes, let me attach the file ... I am new to contributing to GitHub, so I am not yet used to how to do it. Sorry. I also do have a "Handzentrale" (9493, identifier E0), but don't yet know what to do with it within HA, so haven't done any investigations yet. Ideas are welcome.

binary_sensor.py.zip

gluap commented 1 year ago

@lrusdorf the new pre-release contains your code (slightly adapted to merge with #28). The normal procedure here would be a pull request, but as it was only one file sending that via attachement requires less work and reading up on procedure on your part.

gluap commented 9 months ago

code was added