Skip to content

Improve configuration schema for Geniushub integration#23155

Merged
MartinHjelmare merged 4 commits into
home-assistant:devfrom
zxdavb:separate_host_from_token
Apr 18, 2019
Merged

Improve configuration schema for Geniushub integration#23155
MartinHjelmare merged 4 commits into
home-assistant:devfrom
zxdavb:separate_host_from_token

Conversation

@zxdavb
Copy link
Copy Markdown
Member

@zxdavb zxdavb commented Apr 17, 2019

Breaking Change:

Nil, original PR, #21598, is still in dev

Description:

See config section, below.

Pull request in home-assistant.io with documentation (if applicable): home-assistant/home-assistant.io#8815

Example entry for configuration.yaml (if applicable):

As per @MartinHjelmare request, config is now be either of:

geniushub:
  token: skdjflksjfsljd...

... or ...

geniushub:
  host: 192.168.0.2
  username: my_username
  password: my password

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.

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

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

  • N/A The manifest file has all fields filled out correctly (example).
  • N/A New dependencies have been added to requirements in the manifest (example).
  • N/A New dependencies are only imported inside functions that use them (example).
  • N/A 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:

  • N/A - Tests have been added to verify that the new code works.

@zxdavb zxdavb changed the title Configuration for hub tokens is now separate from host addresses Improve configuration schema for Geniushub integration Apr 17, 2019
Comment thread homeassistant/components/geniushub/__init__.py
username = hass_config[DOMAIN].get(CONF_USERNAME)
password = hass_config[DOMAIN].get(CONF_PASSWORD)
else:
host = hass_config[DOMAIN].get(CONF_TOKEN)
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.

Don't call this host, when it's not a host.

Eg collect the arguments in a tuple and pass the tuple to the class.

Copy link
Copy Markdown
Member Author

@zxdavb zxdavb Apr 17, 2019

Choose a reason for hiding this comment

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

Which do you prefer:

    host = token = None
    if CONF_HOST in hass_config[DOMAIN]:
        host = hass_config[DOMAIN].get(CONF_HOST)
        username = hass_config[DOMAIN].get(CONF_USERNAME)
        password = hass_config[DOMAIN].get(CONF_PASSWORD)
    else:
        token = hass_config[DOMAIN].get(CONF_TOKEN)
        username = password = None

    try:
        client = geniushub_data['client'] = GeniusHubClient(
            host if host else token, username, password,
            session=async_get_clientsession(hass)
        )

or

    if CONF_HOST in hass_config[DOMAIN]:
        hub_identifier = hass_config[DOMAIN].get(CONF_HOST)
        username = hass_config[DOMAIN].get(CONF_USERNAME)
        password = hass_config[DOMAIN].get(CONF_PASSWORD)
    else:
        hub_identifier = hass_config[DOMAIN].get(CONF_TOKEN)
        username = password = None

    try:
        client = geniushub_data['client'] = GeniusHubClient(
            hub_identifier, username, password,
            session=async_get_clientsession(hass)
        )

or

    host = hass_config[DOMAIN].get(CONF_HOST)
    username = hass_config[DOMAIN].get(CONF_USERNAME)
    password = hass_config[DOMAIN].get(CONF_PASSWORD)
    token = hass_config[DOMAIN].get(CONF_TOKEN)

    try:
        client = geniushub_data['client'] = GeniusHubClient(
            host if host else token, username, password,
            session=async_get_clientsession(hass)
        )

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.

Collect the arguments in a tuple.

if blabla:
    args = arg1, arg2, etc
else:
    args = (arg4, )
client = class(*args, session=session)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh, I see - but it would a tuple of 1 (hub_id), the rest are kwargs/dict. I don't see what would be gained:

def __init__(self, hub_id, username=None, password=None, session=None, debug=False)

So how about:

    kwargs = dict(hass_config[DOMAIN])
    if CONF_HOST in kwargs:
        args = (kwargs.pop(CONF_HOST), )
    else:
        args = (kwargs.pop(CONF_TOKEN), )

    try:
        client = geniushub_data['client'] = GeniusHubClient(
            *args, **kwargs, session=async_get_clientsession(hass)
        )

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.

We gain that we don't confuse ourselves by naming a variable something which it isn't.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

👍 In addition, I think it's kind of elegant - you get a two-for-one from voluptuous.

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.

Can be merged when last comment is addressed.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 18, 2019

Codecov Report

Merging #23155 into dev will increase coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev   #23155      +/-   ##
==========================================
+ Coverage   94.19%    94.2%   +<.01%     
==========================================
  Files         453      453              
  Lines       36915    36915              
==========================================
+ Hits        34773    34775       +2     
+ Misses       2142     2140       -2
Impacted Files Coverage Δ
homeassistant/components/uk_transport/sensor.py 93.43% <0%> (-0.73%) ⬇️
...ssistant/components/islamic_prayer_times/sensor.py 97.87% <0%> (+3.19%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f6349a6...48484c8. Read the comment docs.

2 similar comments
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 18, 2019

Codecov Report

Merging #23155 into dev will increase coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev   #23155      +/-   ##
==========================================
+ Coverage   94.19%    94.2%   +<.01%     
==========================================
  Files         453      453              
  Lines       36915    36915              
==========================================
+ Hits        34773    34775       +2     
+ Misses       2142     2140       -2
Impacted Files Coverage Δ
homeassistant/components/uk_transport/sensor.py 93.43% <0%> (-0.73%) ⬇️
...ssistant/components/islamic_prayer_times/sensor.py 97.87% <0%> (+3.19%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f6349a6...48484c8. Read the comment docs.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 18, 2019

Codecov Report

Merging #23155 into dev will increase coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev   #23155      +/-   ##
==========================================
+ Coverage   94.19%    94.2%   +<.01%     
==========================================
  Files         453      453              
  Lines       36915    36915              
==========================================
+ Hits        34773    34775       +2     
+ Misses       2142     2140       -2
Impacted Files Coverage Δ
homeassistant/components/uk_transport/sensor.py 93.43% <0%> (-0.73%) ⬇️
...ssistant/components/islamic_prayer_times/sensor.py 97.87% <0%> (+3.19%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f6349a6...48484c8. Read the comment docs.

@MartinHjelmare MartinHjelmare merged commit 11fb486 into home-assistant:dev Apr 18, 2019
@MartinHjelmare MartinHjelmare added this to the 0.92.0 milestone Apr 18, 2019
@MartinHjelmare
Copy link
Copy Markdown
Member

The first PR for this integration is already in 0.92 beta, so tagging this for 0.92 too.

@zxdavb
Copy link
Copy Markdown
Member Author

zxdavb commented Apr 18, 2019

Woot! Merged!

Thanks @MartinHjelmare 💐 @GeoffAtHome 🥇

balloob pushed a commit that referenced this pull request Apr 20, 2019
* configuration for hub tokens are now separate from host addresses/credentials

* small change to docstring

* use *args **kwargs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants