Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hue: add support for username configuration option #3766

Closed

Conversation

craigcabrey
Copy link

@craigcabrey craigcabrey commented Oct 8, 2016

Description:
Allow a Hue bridge username to be specified in configuration.yaml. This removes the external dependency on the Phue configuration. The motivation here is to keep secrets all in secrets.yaml.

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

Example entry for configuration.yaml (if applicable):

light:
  platform: hue
  host: {{bridge address}}
  allow_unreachable: false
  username: !secret hue_username

Checklist:

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

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

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New 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:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

This option will remove the need to specify an external configuration
(though this change doesn't stop you from doing that if you still like).

@mention-bot
Copy link

@craigcabrey, thanks for your PR! By analyzing the history of the files in this pull request, we identified @fabaff, @balloob and @rhooper to be potential reviewers.

"""Setup a phue bridge based on host parameter."""
import phue

try:
bridge = phue.Bridge(
host,
username=username,
config_file_path=hass.config.path(filename))
except ConnectionRefusedError: # Wrong host was given
_LOGGER.error("Error connecting to the Hue bridge at %s", host)
Copy link

Choose a reason for hiding this comment

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

Could be beneficial to add username in here? So it can give a more fruitful message for the user to fix the problem. 👍

Copy link
Member

Choose a reason for hiding this comment

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

Problem with outputting the username that it is actually an auth token (user+pass in 1), there is no password.

@@ -22,7 +22,8 @@
FLASH_LONG, FLASH_SHORT, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP,
SUPPORT_EFFECT, SUPPORT_FLASH, SUPPORT_RGB_COLOR, SUPPORT_TRANSITION,
SUPPORT_XY_COLOR, Light, PLATFORM_SCHEMA)
from homeassistant.const import (CONF_FILENAME, CONF_HOST, DEVICE_DEFAULT_NAME)
from homeassistant.const import (
CONF_FILENAME, CONF_HOST, CONF_USERNAME, DEVICE_DEFAULT_NAME)
Copy link
Member

@pvizeli pvizeli Oct 9, 2016

Choose a reason for hiding this comment

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

Add username option also to voluptuous

def test_setup_platform_uses_discovery_info(self,
socket_mock,
setup_bridge_mock):
hue.setup_platform(
Copy link
Member

Choose a reason for hiding this comment

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

use component_setup from bootstrap

Copy link
Author

Choose a reason for hiding this comment

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

component_setup() is scoped inside of from_config_dict() in homeassistant.bootstrap. Did you mean setup_component()?

Copy link
Member

Choose a reason for hiding this comment

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

👍

del self.config[CONF_HOST]

self.assertFalse(
hue.setup_platform(
Copy link
Member

Choose a reason for hiding this comment

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

use component_setup from bootstrap


hue._CONFIGURED_BRIDGES['test'] = True

hue.setup_platform(
Copy link
Member

Choose a reason for hiding this comment

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

use component_setup from bootstrap


hue._CONFIGURING['test'] = True

hue.setup_platform(
Copy link
Member

Choose a reason for hiding this comment

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

use component_setup from bootstrap

def test_setup_platform(self, socket_mock, setup_bridge_mock):
socket_mock.return_value = 'test'

hue.setup_platform(
Copy link
Member

Choose a reason for hiding this comment

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

use component_setup from bootstrap

@pvizeli
Copy link
Member

pvizeli commented Oct 10, 2016

Have you test you code in your env? This code work only with you unittest but will not work on a real config in first state who had open the PR.

@craigcabrey
Copy link
Author

@pvizeli I'm not sure what you mean. I'm running with these changes right now. username is None if it isn't specified in the config, which will just be passed to phue.Bridge (which is fine with a NoneType).

@craigcabrey
Copy link
Author

@pvizeli can you provide any clarification? I'm eager to get this in mergable (mergeable?) shape 😄

@@ -51,6 +52,7 @@
vol.Required(CONF_HOST): cv.string,
Copy link
Member

@balloob balloob Oct 14, 2016

Choose a reason for hiding this comment

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

Whilte testing your PR I realized that this one should actually be vol.Optional. Could you change that ?

Copy link
Author

Choose a reason for hiding this comment

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

👍

@balloob
Copy link
Member

balloob commented Oct 14, 2016

Tested locally, code works with config files.

Check Travis logs, it actually found a bug 👍 Line 199 calls setup_bridge.

You can disable the pylint warning for too many arguments.

@craigcabrey craigcabrey force-pushed the feat/hue-username-config branch from 0afa630 to 2322ce2 Compare October 14, 2016 05:27
@craigcabrey
Copy link
Author

Is setup_bridge not supposed to be called? It looks correct. Perhaps I'm misunderstanding the problem?

@balloob
Copy link
Member

balloob commented Oct 15, 2016

@craigcabrey The call to setup_bridge in line 199 does not incorporate the newly added username parameter.

This option will remove the need to specify an external configuration
(though this change doesn't stop you from doing that if you still like).
@craigcabrey craigcabrey force-pushed the feat/hue-username-config branch from 2322ce2 to cba76a8 Compare October 15, 2016 19:40
@pvizeli
Copy link
Member

pvizeli commented Oct 16, 2016

Don't use hue.setup_platform in unitest. Use bootstrap.setup_component with a config dict. See on other tests how that work...

@craigcabrey
Copy link
Author

@pvizeli I'd argue that those tests are not ideal. I'm looking at bootstrap.setup_component and there's far too much going on for it to be used as an entry point for a unit test.

It would essentially invalidate the unit test results -- there would be too much noise for very little signal. Unless you're thinking of these as integration tests, I'd strongly recommend keeping the tests scoped to as little code as possible.

However, if you and @balloob insist on this, then I'll be happy to oblige.

@pvizeli
Copy link
Member

pvizeli commented Oct 16, 2016

    def test_template(self):
        """Test template."""
        with assert_setup_component(1):
            assert setup_component(self.hass, 'sensor', {
                'sensor': {
                    'platform': 'template',
                    'sensors': {
                        'test_template_sensor': {
                            'value_template':
                                "It {{ states.sensor.test_state.state }}."
                        }
                    }
                }
            })

        state = self.hass.states.get('sensor.test_template_sensor')
        assert state.state == 'It .'

        self.hass.states.set('sensor.test_state', 'Works')
        self.hass.block_till_done()
        state = self.hass.states.get('sensor.test_template_sensor')
        assert state.state == 'It Works.'

    def test_template_syntax_error(self):
        """Test templating syntax error."""
        with assert_setup_component(0):
            assert setup_component(self.hass, 'sensor', {
                'sensor': {
                    'platform': 'template',
                    'sensors': {
                        'test_template_sensor': {
                            'value_template':
                                "{% if rubbish %}"
                        }
                    }
                }
            })
        assert self.hass.states.all() == []

@balloob
Copy link
Member

balloob commented Oct 16, 2016

The reason we have been migrating tests to use setup_comonent is that it verifies that the setup works correctly together with PLATFORM_SCHEMA. I'm also fine with adding 1 extra test that uses setup_component.

It also seems like you have 2 lint issues left. PyLint is a bit too strict here so you can disable them by adding this comment # pylint: disable=too-many-arguments

************* Module homeassistant.components.light.hue
R:111, 0: Too many arguments (6/5) (too-many-arguments)
R:184, 0: Too many arguments (6/5) (too-many-arguments)

@balloob
Copy link
Member

balloob commented Nov 16, 2016

This PR seems to have gone stale. Closing it. You can reopen it when you're ready to finish it.

@balloob balloob closed this Nov 16, 2016
@home-assistant home-assistant locked and limited conversation to collaborators Mar 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants