jpcornil-git / HA-mcp23017

MCP23017 implementation for Home Assistant (threadsafe, lower latency and config flow support)
18 stars 7 forks source link

MCP23017 as a sensors problem with right status (binary sensor at HA) #1

Closed bigu1975 closed 3 years ago

bigu1975 commented 3 years ago

Hello,

First of all I would like to thank you very much for your contribution. I have been searching such implementation since months. Finally I found it here. I have just tested it at HA core-2021.6.2 (the newest version). In overall I have attached 4 MCP23017 - two acts as Switches and 2 acts as Sensors. Switches works fine but I have huge problem with Sensors. I have manually installed your mod by adding mcp23017 directory to custom components HA folder (keeping the structure). Problem starts just after HA restart - when all inputs are LOW (without inverted logic with activated pull ups) for some pins HA shows incorrect status - HIGH. This happens every time (on every HA start/restart and always for the same pins). After playing with changing inputs HA reflects statuses changes and starts shows it correctly. Of course I need to change it twice to get correct status (it is related to wrongly set pins after restart). But... if the change only happens on the single pin it works great (for all pins). But If I am changing at once few pins (for instance I am connecting outputs from 2 first mcps as switches to inputs of sensors and via script I am changing statuses of all pins - HA for sensors are not reflecting changes on some pins. It happens on the same pins, which shows incorrect status after HA restart. Exactly the same settings works like a charm on default HA MCP23017 integration. I am using the following settings for sensors - and so far I remember at least one pin which have problem: pin6 on the first chip.

Thank you in advance for any help. This mcp implementation is far better. For me most important is lack of glitches during restarts.

Kind regards, Bartosz

jpcornil-git commented 3 years ago

Hi Bartoz,

Thanks for you report, based on your description it seems that the initial update is ignored. Could you comment out the following line (it shouldn't be there ... leftover from an older implementation):

https://github.com/jpcornil-git/HA-mcp23017/blob/main/custom_components/mcp23017/__init__.py#L366

You may also enable logging for this module in your configuration.yaml if the above doesn't fix the issue and report HA logs.

logger: default: warning logs: custom_components.mcp23017: debug

Cheers, jpc

bigu1975 commented 3 years ago

I don't know what to say and how I can thank you. Indeed it makes the trick. Now works perfectly. There is a huge improvement in regards to "native" HA mcp23017 support.

Again thank you very, very much.

bigu1975 commented 3 years ago

I would like to ask about one more thing. If I may... And I know is not an issue but rather ask for help in implementing change...

For my settings I need to quite different switch behavior. My target is to have following workflow for switches:

doesn't matter what I send to switch from HA - either On or Off I would like the switch together with pin will change its state to opposite. Even if the pin is low and status is Off when I send Off it will change the state to On and vice versa. For current mcp23017 HA implementation I have achieved it by the following changes in switch.py:

def turn_on(self, **kwargs): """Turn the device on.""" self._pin.value = not self._pin.value self._state = self.pin.value self.schedule_update_ha_state()

def turn_off(self, **kwargs): """Turn the device off.""" self._pin.value = not self._pin.value self._state = self.pin.value self.schedule_update_ha_state() Here I can't simply achieve it. I would be very grateful for support in this subject.

bigu1975 commented 3 years ago

And Issue for switches. Again two mcp acting as switches. After fresh HA restart when I activate script to switch all switches on or off for both MCP, at the first time only first 8 outputs changes the state (pins from 0 to 7). After second time everything works for bot mcp.

Edit: Ok after fresh reboot pins from 0-7 are set to High, 8-15 as low on both MCP (invert_logic set to true). It is a problem wrong init (maybe).

For me the solution would be previous question in previous post. For other - not.

Thank you.

jpcornil-git commented 3 years ago

Hi Bartoz,,

Glad to see that the change solves your issue, I'll checkin the fix.

Wrt switch init behavior, I'm not sure I understand your description; after HA reboot the switch state shouldn't change, i.e. it should be the same as before the reboot (or mpc reset value), there is no reinitialization (if light/pump/... was on you want to keep everything as-is), see

https://github.com/jpcornil-git/HA-mcp23017/blob/main/custom_components/mcp23017/switch.py#L210

Note that a switch has a toggle primitive (https://www.home-assistant.io/integrations/switch/) => no need apriori to change the code ?

Cheers, jpc

bigu1975 commented 3 years ago

Let's focus on init problem: My config:

And you are right - after the HA restart switches keep its statuses but again only for register A (first half). Second half (register B) always shows off - and I know this is fake status - and only status. Real pin is set correctly

jpcornil-git commented 3 years ago

Hi Bartoz, Could you open another issue to track this one (I'll have a look in the mean time) Cheers jpc

bigu1975 commented 3 years ago

It is done. Thank you - really.