def google_config(self) -> ga_h.Config: """Return Google config.""" if not self._google_config: google_conf = self._google_user_config def should_expose(entity): """If an entity should be exposed.""" if entity.entity_id in CLOUD_NEVER_EXPOSED_ENTITIES: return False return google_conf['filter'](entity.entity_id) username = self._hass.data[DOMAIN].claims["cognito:username"] self._google_config = ga_h.Config( should_expose=should_expose, secure_devices_pin=self._prefs.google_secure_devices_pin, entity_config=google_conf.get(CONF_ENTITY_CONFIG), agent_user_id=username, ) # Set it to the latest. self._google_config.secure_devices_pin = \ self._prefs.google_secure_devices_pin return self._google_config
def gactions_config(self): """Return the Google Assistant config.""" if self._gactions_config is None: conf = self._google_actions def should_expose(entity): """If an entity should be exposed.""" return conf['filter'](entity.entity_id) self._gactions_config = ga_h.Config( should_expose=should_expose, agent_user_id=self.claims['cognito:username'], entity_config=conf.get(CONF_ENTITY_CONFIG), ) return self._gactions_config
def google_config(self) -> ga_h.Config: """Return Google config.""" if not self._google_config: google_conf = self._google_user_config def should_expose(entity): """If an entity should be exposed.""" if entity.entity_id in CLOUD_NEVER_EXPOSED_ENTITIES: return False return google_conf['filter'](entity.entity_id) self._google_config = ga_h.Config( should_expose=should_expose, allow_unlock=self._prefs.google_allow_unlock, entity_config=google_conf.get(CONF_ENTITY_CONFIG), ) return self._google_config
def gactions_config(self): """Return the Google Assistant config.""" if self._gactions_config is None: conf = self.google_actions_user_conf def should_expose(entity): """If an entity should be exposed.""" if entity.entity_id in CLOUD_NEVER_EXPOSED_ENTITIES: return False return conf['filter'](entity.entity_id) self._gactions_config = ga_h.Config( should_expose=should_expose, allow_unlock=self.prefs.google_allow_unlock, agent_user_id=self.claims['cognito:username'], entity_config=conf.get(CONF_ENTITY_CONFIG), ) return self._gactions_config
def google_config(self) -> ga_h.Config: """Return Google config.""" if not self._google_config: google_conf = self._google_user_config def should_expose(entity): """If an entity should be exposed.""" if entity.entity_id in CLOUD_NEVER_EXPOSED_ENTITIES: return False if not google_conf['filter'].empty_filter: return google_conf['filter'](entity.entity_id) entity_configs = self.prefs.google_entity_configs entity_config = entity_configs.get(entity.entity_id, {}) return entity_config.get(PREF_SHOULD_EXPOSE, DEFAULT_SHOULD_EXPOSE) def should_2fa(entity): """If an entity should be checked for 2FA.""" entity_configs = self.prefs.google_entity_configs entity_config = entity_configs.get(entity.entity_id, {}) return not entity_config.get(PREF_DISABLE_2FA, DEFAULT_DISABLE_2FA) username = self._hass.data[DOMAIN].claims["cognito:username"] self._google_config = ga_h.Config( should_expose=should_expose, should_2fa=should_2fa, secure_devices_pin=self._prefs.google_secure_devices_pin, entity_config=google_conf.get(CONF_ENTITY_CONFIG), agent_user_id=username, ) # Set it to the latest. self._google_config.secure_devices_pin = \ self._prefs.google_secure_devices_pin return self._google_config
async def test_sync_message(hass): """Test a sync message.""" light = DemoLight(None, 'Demo Light', state=False, rgb=[237, 224, 33]) light.hass = hass light.entity_id = 'light.demo_light' await light.async_update_ha_state() # This should not show up in the sync request hass.states.async_set('sensor.no_match', 'something') # Excluded via config hass.states.async_set('light.not_expose', 'on') config = helpers.Config( should_expose=lambda state: state.entity_id != 'light.not_expose', agent_user_id='test-agent', entity_config={ 'light.demo_light': { const.CONF_ROOM_HINT: 'Living Room', const.CONF_ALIASES: ['Hello', 'World'] } }) result = await sh.async_handle_message( hass, config, { "requestId": REQ_ID, "inputs": [{ "intent": "action.devices.SYNC" }] }) assert result == { 'requestId': REQ_ID, 'payload': { 'agentUserId': 'test-agent', 'devices': [{ 'id': 'light.demo_light', 'name': { 'name': 'Demo Light', 'nicknames': [ 'Hello', 'World', ] }, 'traits': [ trait.TRAIT_BRIGHTNESS, trait.TRAIT_ONOFF, trait.TRAIT_COLOR_SPECTRUM, trait.TRAIT_COLOR_TEMP, ], 'type': sh.TYPE_LIGHT, 'willReportState': False, 'attributes': { 'colorModel': 'rgb', 'temperatureMinK': 6493, 'temperatureMaxK': 2000, }, 'roomHint': 'Living Room' }] } }
"""Test Google Smart Home.""" from homeassistant.core import State from homeassistant.const import (ATTR_SUPPORTED_FEATURES, ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS) from homeassistant.setup import async_setup_component from homeassistant.components import climate from homeassistant.components.google_assistant import (const, trait, helpers, smart_home as sh) from homeassistant.components.light.demo import DemoLight BASIC_CONFIG = helpers.Config( should_expose=lambda state: True, agent_user_id='test-agent', ) REQ_ID = 'ff36a3cc-ec34-11e6-b1a0-64510650abcf' async def test_sync_message(hass): """Test a sync message.""" light = DemoLight(None, 'Demo Light', state=False, rgb=[237, 224, 33]) light.hass = hass light.entity_id = 'light.demo_light' await light.async_update_ha_state() # This should not show up in the sync request hass.states.async_set('sensor.no_match', 'something') # Excluded via config hass.states.async_set('light.not_expose', 'on') config = helpers.Config(
async def test_sync_message(hass): """Test a sync message.""" light = DemoLight( None, 'Demo Light', state=False, hs_color=(180, 75), ) light.hass = hass light.entity_id = 'light.demo_light' await light.async_update_ha_state() # This should not show up in the sync request hass.states.async_set('sensor.no_match', 'something') # Excluded via config hass.states.async_set('light.not_expose', 'on') config = helpers.Config( should_expose=lambda state: state.entity_id != 'light.not_expose', entity_config={ 'light.demo_light': { const.CONF_ROOM_HINT: 'Living Room', const.CONF_ALIASES: ['Hello', 'World'] } }) events = [] hass.bus.async_listen(EVENT_SYNC_RECEIVED, events.append) result = await sh.async_handle_message( hass, config, 'test-agent', { "requestId": REQ_ID, "inputs": [{ "intent": "action.devices.SYNC" }] }) assert result == { 'requestId': REQ_ID, 'payload': { 'agentUserId': 'test-agent', 'devices': [{ 'id': 'light.demo_light', 'name': { 'name': 'Demo Light', 'nicknames': [ 'Hello', 'World', ] }, 'traits': [ trait.TRAIT_BRIGHTNESS, trait.TRAIT_ONOFF, trait.TRAIT_COLOR_SETTING, ], 'type': const.TYPE_LIGHT, 'willReportState': False, 'attributes': { 'colorModel': 'hsv', 'colorTemperatureRange': { 'temperatureMinK': 2000, 'temperatureMaxK': 6535, } }, 'roomHint': 'Living Room' }] } } await hass.async_block_till_done() assert len(events) == 1 assert events[0].event_type == EVENT_SYNC_RECEIVED assert events[0].data == { 'request_id': REQ_ID, }
async def test_sync_in_area(hass, registries): """Test a sync message where room hint comes from area.""" area = registries.area.async_create("Living Room") device = registries.device.async_get_or_create( config_entry_id='1234', connections={(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')}) registries.device.async_update_device(device.id, area_id=area.id) entity = registries.entity.async_get_or_create( 'light', 'test', '1235', suggested_object_id='demo_light', device_id=device.id) light = DemoLight( None, 'Demo Light', state=False, hs_color=(180, 75), ) light.hass = hass light.entity_id = entity.entity_id await light.async_update_ha_state() config = helpers.Config(should_expose=lambda _: True, entity_config={}) events = [] hass.bus.async_listen(EVENT_SYNC_RECEIVED, events.append) result = await sh.async_handle_message( hass, config, 'test-agent', { "requestId": REQ_ID, "inputs": [{ "intent": "action.devices.SYNC" }] }) assert result == { 'requestId': REQ_ID, 'payload': { 'agentUserId': 'test-agent', 'devices': [{ 'id': 'light.demo_light', 'name': { 'name': 'Demo Light' }, 'traits': [ trait.TRAIT_BRIGHTNESS, trait.TRAIT_ONOFF, trait.TRAIT_COLOR_SETTING, ], 'type': const.TYPE_LIGHT, 'willReportState': False, 'attributes': { 'colorModel': 'hsv', 'colorTemperatureRange': { 'temperatureMinK': 2000, 'temperatureMaxK': 6535, } }, 'roomHint': 'Living Room' }] } } await hass.async_block_till_done() assert len(events) == 1 assert events[0].event_type == EVENT_SYNC_RECEIVED assert events[0].data == { 'request_id': REQ_ID, }
from homeassistant.components.google_assistant import (const, trait, helpers, smart_home as sh, EVENT_COMMAND_RECEIVED, EVENT_QUERY_RECEIVED, EVENT_SYNC_RECEIVED) from homeassistant.components.demo.binary_sensor import DemoBinarySensor from homeassistant.components.demo.cover import DemoCover from homeassistant.components.demo.light import DemoLight from homeassistant.components.demo.media_player import AbstractDemoPlayer from homeassistant.components.demo.switch import DemoSwitch from homeassistant.helpers import device_registry from tests.common import (mock_device_registry, mock_registry, mock_area_registry, mock_coro) BASIC_CONFIG = helpers.Config(should_expose=lambda state: True, ) REQ_ID = 'ff36a3cc-ec34-11e6-b1a0-64510650abcf' @pytest.fixture def registries(hass): """Registry mock setup.""" from types import SimpleNamespace ret = SimpleNamespace() ret.entity = mock_registry(hass) ret.device = mock_device_registry(hass) ret.area = mock_area_registry(hass) return ret async def test_sync_message(hass):
from homeassistant.setup import async_setup_component from homeassistant.components.climate.const import (ATTR_MIN_TEMP, ATTR_MAX_TEMP, STATE_HEAT, SUPPORT_OPERATION_MODE) from homeassistant.components.google_assistant import (const, trait, helpers, smart_home as sh, EVENT_COMMAND_RECEIVED, EVENT_QUERY_RECEIVED, EVENT_SYNC_RECEIVED) from homeassistant.components.light.demo import DemoLight from homeassistant.helpers import device_registry from tests.common import (mock_device_registry, mock_registry, mock_area_registry) BASIC_CONFIG = helpers.Config(should_expose=lambda state: True, allow_unlock=False) REQ_ID = 'ff36a3cc-ec34-11e6-b1a0-64510650abcf' @pytest.fixture def registries(hass): """Registry mock setup.""" from types import SimpleNamespace ret = SimpleNamespace() ret.entity = mock_registry(hass) ret.device = mock_device_registry(hass) ret.area = mock_area_registry(hass) return ret async def test_sync_message(hass):
script, switch, vacuum, group, ) from homeassistant.components.google_assistant import trait, helpers, const from homeassistant.const import (STATE_ON, STATE_OFF, ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF, TEMP_CELSIUS, TEMP_FAHRENHEIT, ATTR_SUPPORTED_FEATURES) from homeassistant.core import State, DOMAIN as HA_DOMAIN from homeassistant.util import color from tests.common import async_mock_service BASIC_CONFIG = helpers.Config( should_expose=lambda state: True, agent_user_id='test-agent', ) UNSAFE_CONFIG = helpers.Config( should_expose=lambda state: True, agent_user_id='test-agent', allow_unlock=True, ) async def test_brightness_light(hass): """Test brightness trait support for light domain.""" assert trait.BrightnessTrait.supported(light.DOMAIN, light.SUPPORT_BRIGHTNESS) trt = trait.BrightnessTrait(
group, ) from homeassistant.components.climate import const as climate from homeassistant.components.google_assistant import (trait, helpers, const, error) from homeassistant.const import (STATE_ON, STATE_OFF, ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF, TEMP_CELSIUS, TEMP_FAHRENHEIT, ATTR_SUPPORTED_FEATURES, ATTR_TEMPERATURE, ATTR_DEVICE_CLASS, ATTR_ASSUMED_STATE, STATE_UNKNOWN) from homeassistant.core import State, DOMAIN as HA_DOMAIN, EVENT_CALL_SERVICE from homeassistant.util import color from tests.common import async_mock_service, mock_coro BASIC_CONFIG = helpers.Config(should_expose=lambda state: True, ) REQ_ID = 'ff36a3cc-ec34-11e6-b1a0-64510650abcf' BASIC_DATA = helpers.RequestData( BASIC_CONFIG, 'test-agent', REQ_ID, ) PIN_CONFIG = helpers.Config(should_expose=lambda state: True, secure_devices_pin='1234') PIN_DATA = helpers.RequestData( PIN_CONFIG, 'test-agent',