Skip to content

Fix insteon Hub v1 support#16472

Merged
MartinHjelmare merged 7 commits intohome-assistant:devfrom
teharris1:HubV1
Sep 10, 2018
Merged

Fix insteon Hub v1 support#16472
MartinHjelmare merged 7 commits intohome-assistant:devfrom
teharris1:HubV1

Conversation

@teharris1
Copy link
Copy Markdown
Contributor

@teharris1 teharris1 commented Sep 7, 2018

Description:

When insteon_local and insteon_plm were merged, support for the Insteon Hub version 1 is broken. This fixes that support.

Related issue (if applicable): fixes #16342
Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#6211

Example entry for configuration.yaml (if applicable):

# Hub 2242 configuration variables
insteon:
  host: HOST
  ip_port: IP_PORT
  hub_version: 1

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New or updated dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

CONF_OVERRIDE = 'device_override'
CONF_PLM_HUB_MSG = ('Must configure either a PLM port or a Hub host, username '
'and password')
CONF_PLM_HUB_MSG = ('Must configure either a PLM port or a Hub host')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parenthesis isn't needed.


# Set the default value of the IP port based on Hub version if not
# explicitly set
if not ip_port:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract this into a config validator function, and use it in the config schema.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this was a good exercise. Figured it out but it was not obvious. Thanks for pushing me to do the right thing. You are really good at helping average developers like myself get to the right solution. I really appreciate it.

@teharris1 teharris1 changed the title Fix Hub v1 support [WIP] Fix Hub v1 support Sep 7, 2018
@teharris1 teharris1 changed the title [WIP] Fix Hub v1 support Fix Hub v1 support Sep 8, 2018
elif not ip_port:
obj[CONF_IP_PORT] = 25105
except KeyError:
raise vol.Invalid('Schema must contain {}.'.format(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't happen, since there's a default version.

"""Set the default port based on the Hub version."""
def set_default(obj: Dict) -> Dict:
"""Set ip_port default value based on hub_version."""
if not isinstance(obj, dict):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't happen. The schema already validates the value to a dict.

try:
# If the ip_port is found do nothing
# If it is not found (KeyError) the set the default
ip_port = obj[CONF_IP_PORT]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use dict.get.

ip_port = obj.get(CONF_IP_PORT)
if ip_port is None:
    hub_version = obj[CONF_HUB_VERSION]
    if hub_version == 1:
        obj[CONF_IP_PORT] = 9761
    else:
        obj[CONF_IP_PORT] = 25105
return obj


# Adapted from:
# https://github.com/alecthomas/voluptuous/issues/115#issuecomment-144464666
def set_default_port() -> Callable:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to have this nesting, since we don't need to pass in any other arguments than the dict we want to validate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is correct. The entire schema has to be passed to the method. I don't see how to pass this if it is not nested. I am following the same pattern as has_at_least_one_key. Why would this be different?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind. I see what you mean now. Sorry.

import asyncio
import collections
import logging
from typing import Callable, Dict
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'typing.Callable' imported but unused

# Found hub_version but not ip_port
if hub_version == 1:
schema[CONF_IP_PORT] = 9761
elif not ip_port:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else: is enough here since we already checked that port is falsy above.

vol.Exclusive(CONF_HOST, 'plm_or_hub',
msg=CONF_PLM_HUB_MSG): cv.string,
vol.Optional(CONF_IP_PORT, default=25105): int,
vol.Optional(CONF_IP_PORT): int,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use cv.port.

Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@MartinHjelmare
Copy link
Copy Markdown
Member

Can be merged when build passes.

@MartinHjelmare MartinHjelmare changed the title Fix Hub v1 support Fix insteon Hub v1 support Sep 8, 2018
@teharris1
Copy link
Copy Markdown
Contributor Author

@MartinHjelmare I have a release management question. Since 0.78.0b0 was just released, What does that mean for this fix? Does that mean it can be part of 0.78.0 or will it mean it has to wait until 0.79 or can it be included in 0.77.4 if one is released? Keep in mind this functionality was working prior to 0.77 for insteon_local users so I want to get them back up and running. Appreciate all your help.

@MartinHjelmare
Copy link
Copy Markdown
Member

We can tag it for 0.78.

@MartinHjelmare MartinHjelmare added this to the 0.78.0 milestone Sep 10, 2018
@MartinHjelmare MartinHjelmare merged commit dcd7b9a into home-assistant:dev Sep 10, 2018
@ghost ghost removed the in progress label Sep 10, 2018
@teharris1 teharris1 deleted the HubV1 branch September 11, 2018 01:22
balloob pushed a commit that referenced this pull request Sep 11, 2018
* Fix support for Hub version 1 (i.e. pre-2014 Hub model 2242)

* Bump insteonplm to 0.14.1

* Code review changes

* Clean up and better document set_default_port

* Simplify set_default_port based on code review

* Remove Callable type import

* Simplify port setup
@balloob balloob mentioned this pull request Sep 17, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Feb 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Insteon component stopped working with introduction of home assistant 0.77

5 participants