Search is not available for this dataset
identifier
stringlengths
1
155
parameters
stringlengths
2
6.09k
docstring
stringlengths
11
63.4k
docstring_summary
stringlengths
0
63.4k
function
stringlengths
29
99.8k
function_tokens
sequence
start_point
sequence
end_point
sequence
language
stringclasses
1 value
docstring_language
stringlengths
2
7
docstring_language_predictions
stringlengths
18
23
is_langid_reliable
stringclasses
2 values
logbook_filtering_state
(hass)
Filter state changes.
Filter state changes.
async def logbook_filtering_state(hass): """Filter state changes.""" return await _logbook_filtering(hass, 1, 1)
[ "async", "def", "logbook_filtering_state", "(", "hass", ")", ":", "return", "await", "_logbook_filtering", "(", "hass", ",", "1", ",", "1", ")" ]
[ 188, 0 ]
[ 190, 47 ]
python
en
['en', 'en', 'en']
True
logbook_filtering_attributes
(hass)
Filter attribute changes.
Filter attribute changes.
async def logbook_filtering_attributes(hass): """Filter attribute changes.""" return await _logbook_filtering(hass, 1, 2)
[ "async", "def", "logbook_filtering_attributes", "(", "hass", ")", ":", "return", "await", "_logbook_filtering", "(", "hass", ",", "1", ",", "2", ")" ]
[ 194, 0 ]
[ 196, 47 ]
python
en
['en', 'en', 'en']
True
filtering_entity_id
(hass)
Run a 100k state changes through entity filter.
Run a 100k state changes through entity filter.
async def filtering_entity_id(hass): """Run a 100k state changes through entity filter.""" config = { "include": { "domains": [ "automation", "script", "group", "media_player", "custom_component", ], "entity_globs": [ "binary_sensor.*_contact", "binary_sensor.*_occupancy", "binary_sensor.*_detected", "binary_sensor.*_active", "input_*", "device_tracker.*_phone", "switch.*_light", "binary_sensor.*_charging", "binary_sensor.*_lock", "binary_sensor.*_connected", ], "entities": [ "test.entity_1", "test.entity_2", "binary_sensor.garage_door_open", "test.entity_3", "test.entity_4", ], }, "exclude": { "domains": ["input_number"], "entity_globs": ["media_player.google_*", "group.all_*"], "entities": [], }, } entity_ids = [ "automation.home_arrival", "script.shut_off_house", "binary_sensor.garage_door_open", "binary_sensor.front_door_lock", "binary_sensor.kitchen_motion_sensor_occupancy", "switch.desk_lamp", "light.dining_room", "input_boolean.guest_staying_over", "person.eleanor_fant", "alert.issue_at_home", "calendar.eleanor_fant_s_calendar", "sun.sun", ] entities_filter = convert_include_exclude_filter(config) size = len(entity_ids) start = timer() for i in range(10 ** 5): entities_filter(entity_ids[i % size]) return timer() - start
[ "async", "def", "filtering_entity_id", "(", "hass", ")", ":", "config", "=", "{", "\"include\"", ":", "{", "\"domains\"", ":", "[", "\"automation\"", ",", "\"script\"", ",", "\"group\"", ",", "\"media_player\"", ",", "\"custom_component\"", ",", "]", ",", "\"entity_globs\"", ":", "[", "\"binary_sensor.*_contact\"", ",", "\"binary_sensor.*_occupancy\"", ",", "\"binary_sensor.*_detected\"", ",", "\"binary_sensor.*_active\"", ",", "\"input_*\"", ",", "\"device_tracker.*_phone\"", ",", "\"switch.*_light\"", ",", "\"binary_sensor.*_charging\"", ",", "\"binary_sensor.*_lock\"", ",", "\"binary_sensor.*_connected\"", ",", "]", ",", "\"entities\"", ":", "[", "\"test.entity_1\"", ",", "\"test.entity_2\"", ",", "\"binary_sensor.garage_door_open\"", ",", "\"test.entity_3\"", ",", "\"test.entity_4\"", ",", "]", ",", "}", ",", "\"exclude\"", ":", "{", "\"domains\"", ":", "[", "\"input_number\"", "]", ",", "\"entity_globs\"", ":", "[", "\"media_player.google_*\"", ",", "\"group.all_*\"", "]", ",", "\"entities\"", ":", "[", "]", ",", "}", ",", "}", "entity_ids", "=", "[", "\"automation.home_arrival\"", ",", "\"script.shut_off_house\"", ",", "\"binary_sensor.garage_door_open\"", ",", "\"binary_sensor.front_door_lock\"", ",", "\"binary_sensor.kitchen_motion_sensor_occupancy\"", ",", "\"switch.desk_lamp\"", ",", "\"light.dining_room\"", ",", "\"input_boolean.guest_staying_over\"", ",", "\"person.eleanor_fant\"", ",", "\"alert.issue_at_home\"", ",", "\"calendar.eleanor_fant_s_calendar\"", ",", "\"sun.sun\"", ",", "]", "entities_filter", "=", "convert_include_exclude_filter", "(", "config", ")", "size", "=", "len", "(", "entity_ids", ")", "start", "=", "timer", "(", ")", "for", "i", "in", "range", "(", "10", "**", "5", ")", ":", "entities_filter", "(", "entity_ids", "[", "i", "%", "size", "]", ")", "return", "timer", "(", ")", "-", "start" ]
[ 239, 0 ]
[ 300, 26 ]
python
en
['en', 'en', 'en']
True
valid_entity_id
(hass)
Run valid entity ID a million times.
Run valid entity ID a million times.
async def valid_entity_id(hass): """Run valid entity ID a million times.""" start = timer() for _ in range(10 ** 6): core.valid_entity_id("light.kitchen") return timer() - start
[ "async", "def", "valid_entity_id", "(", "hass", ")", ":", "start", "=", "timer", "(", ")", "for", "_", "in", "range", "(", "10", "**", "6", ")", ":", "core", ".", "valid_entity_id", "(", "\"light.kitchen\"", ")", "return", "timer", "(", ")", "-", "start" ]
[ 304, 0 ]
[ 309, 26 ]
python
en
['en', 'cy', 'en']
True
json_serialize_states
(hass)
Serialize million states with websocket default encoder.
Serialize million states with websocket default encoder.
async def json_serialize_states(hass): """Serialize million states with websocket default encoder.""" states = [ core.State("light.kitchen", "on", {"friendly_name": "Kitchen Lights"}) for _ in range(10 ** 6) ] start = timer() JSON_DUMP(states) return timer() - start
[ "async", "def", "json_serialize_states", "(", "hass", ")", ":", "states", "=", "[", "core", ".", "State", "(", "\"light.kitchen\"", ",", "\"on\"", ",", "{", "\"friendly_name\"", ":", "\"Kitchen Lights\"", "}", ")", "for", "_", "in", "range", "(", "10", "**", "6", ")", "]", "start", "=", "timer", "(", ")", "JSON_DUMP", "(", "states", ")", "return", "timer", "(", ")", "-", "start" ]
[ 313, 0 ]
[ 322, 26 ]
python
en
['en', 'da', 'en']
True
_create_state_changed_event_from_old_new
( entity_id, event_time_fired, old_state, new_state )
Create a state changed event from a old and new state.
Create a state changed event from a old and new state.
def _create_state_changed_event_from_old_new( entity_id, event_time_fired, old_state, new_state ): """Create a state changed event from a old and new state.""" attributes = {} if new_state is not None: attributes = new_state.get("attributes") attributes_json = json.dumps(attributes, cls=JSONEncoder) if attributes_json == "null": attributes_json = "{}" row = collections.namedtuple( "Row", [ "event_type" "event_data" "time_fired" "context_id" "context_user_id" "state" "entity_id" "domain" "attributes" "state_id", "old_state_id", ], ) row.event_type = EVENT_STATE_CHANGED row.event_data = "{}" row.attributes = attributes_json row.time_fired = event_time_fired row.state = new_state and new_state.get("state") row.entity_id = entity_id row.domain = entity_id and core.split_entity_id(entity_id)[0] row.context_id = None row.context_user_id = None row.old_state_id = old_state and 1 row.state_id = new_state and 1 # pylint: disable=import-outside-toplevel from homeassistant.components import logbook return logbook.LazyEventPartialState(row)
[ "def", "_create_state_changed_event_from_old_new", "(", "entity_id", ",", "event_time_fired", ",", "old_state", ",", "new_state", ")", ":", "attributes", "=", "{", "}", "if", "new_state", "is", "not", "None", ":", "attributes", "=", "new_state", ".", "get", "(", "\"attributes\"", ")", "attributes_json", "=", "json", ".", "dumps", "(", "attributes", ",", "cls", "=", "JSONEncoder", ")", "if", "attributes_json", "==", "\"null\"", ":", "attributes_json", "=", "\"{}\"", "row", "=", "collections", ".", "namedtuple", "(", "\"Row\"", ",", "[", "\"event_type\"", "\"event_data\"", "\"time_fired\"", "\"context_id\"", "\"context_user_id\"", "\"state\"", "\"entity_id\"", "\"domain\"", "\"attributes\"", "\"state_id\"", ",", "\"old_state_id\"", ",", "]", ",", ")", "row", ".", "event_type", "=", "EVENT_STATE_CHANGED", "row", ".", "event_data", "=", "\"{}\"", "row", ".", "attributes", "=", "attributes_json", "row", ".", "time_fired", "=", "event_time_fired", "row", ".", "state", "=", "new_state", "and", "new_state", ".", "get", "(", "\"state\"", ")", "row", ".", "entity_id", "=", "entity_id", "row", ".", "domain", "=", "entity_id", "and", "core", ".", "split_entity_id", "(", "entity_id", ")", "[", "0", "]", "row", ".", "context_id", "=", "None", "row", ".", "context_user_id", "=", "None", "row", ".", "old_state_id", "=", "old_state", "and", "1", "row", ".", "state_id", "=", "new_state", "and", "1", "# pylint: disable=import-outside-toplevel", "from", "homeassistant", ".", "components", "import", "logbook", "return", "logbook", ".", "LazyEventPartialState", "(", "row", ")" ]
[ 325, 0 ]
[ 367, 45 ]
python
en
['en', 'en', 'en']
True
blueprint_1
()
Blueprint fixture.
Blueprint fixture.
def blueprint_1(): """Blueprint fixture.""" return models.Blueprint( { "blueprint": { "name": "Hello", "domain": "automation", "source_url": "https://github.com/balloob/home-assistant-config/blob/main/blueprints/automation/motion_light.yaml", "input": { "test-placeholder": {"name": "Name", "description": "Description"} }, }, "example": Placeholder("test-placeholder"), } )
[ "def", "blueprint_1", "(", ")", ":", "return", "models", ".", "Blueprint", "(", "{", "\"blueprint\"", ":", "{", "\"name\"", ":", "\"Hello\"", ",", "\"domain\"", ":", "\"automation\"", ",", "\"source_url\"", ":", "\"https://github.com/balloob/home-assistant-config/blob/main/blueprints/automation/motion_light.yaml\"", ",", "\"input\"", ":", "{", "\"test-placeholder\"", ":", "{", "\"name\"", ":", "\"Name\"", ",", "\"description\"", ":", "\"Description\"", "}", "}", ",", "}", ",", "\"example\"", ":", "Placeholder", "(", "\"test-placeholder\"", ")", ",", "}", ")" ]
[ 12, 0 ]
[ 26, 5 ]
python
en
['fr', 'lb', 'en']
False
domain_bps
(hass)
Domain blueprints fixture.
Domain blueprints fixture.
def domain_bps(hass): """Domain blueprints fixture.""" return models.DomainBlueprints(hass, "automation", logging.getLogger(__name__))
[ "def", "domain_bps", "(", "hass", ")", ":", "return", "models", ".", "DomainBlueprints", "(", "hass", ",", "\"automation\"", ",", "logging", ".", "getLogger", "(", "__name__", ")", ")" ]
[ 30, 0 ]
[ 32, 83 ]
python
fr
['fr', 'fr', 'en']
True
test_blueprint_model_init
()
Test constructor validation.
Test constructor validation.
def test_blueprint_model_init(): """Test constructor validation.""" with pytest.raises(errors.InvalidBlueprint): models.Blueprint({}) with pytest.raises(errors.InvalidBlueprint): models.Blueprint( {"blueprint": {"name": "Hello", "domain": "automation"}}, expected_domain="not-automation", ) with pytest.raises(errors.InvalidBlueprint): models.Blueprint( { "blueprint": { "name": "Hello", "domain": "automation", "input": {"something": None}, }, "trigger": {"platform": Placeholder("non-existing")}, } )
[ "def", "test_blueprint_model_init", "(", ")", ":", "with", "pytest", ".", "raises", "(", "errors", ".", "InvalidBlueprint", ")", ":", "models", ".", "Blueprint", "(", "{", "}", ")", "with", "pytest", ".", "raises", "(", "errors", ".", "InvalidBlueprint", ")", ":", "models", ".", "Blueprint", "(", "{", "\"blueprint\"", ":", "{", "\"name\"", ":", "\"Hello\"", ",", "\"domain\"", ":", "\"automation\"", "}", "}", ",", "expected_domain", "=", "\"not-automation\"", ",", ")", "with", "pytest", ".", "raises", "(", "errors", ".", "InvalidBlueprint", ")", ":", "models", ".", "Blueprint", "(", "{", "\"blueprint\"", ":", "{", "\"name\"", ":", "\"Hello\"", ",", "\"domain\"", ":", "\"automation\"", ",", "\"input\"", ":", "{", "\"something\"", ":", "None", "}", ",", "}", ",", "\"trigger\"", ":", "{", "\"platform\"", ":", "Placeholder", "(", "\"non-existing\"", ")", "}", ",", "}", ")" ]
[ 35, 0 ]
[ 56, 9 ]
python
en
['en', 'sv', 'en']
True
test_blueprint_properties
(blueprint_1)
Test properties.
Test properties.
def test_blueprint_properties(blueprint_1): """Test properties.""" assert blueprint_1.metadata == { "name": "Hello", "domain": "automation", "source_url": "https://github.com/balloob/home-assistant-config/blob/main/blueprints/automation/motion_light.yaml", "input": {"test-placeholder": {"name": "Name", "description": "Description"}}, } assert blueprint_1.domain == "automation" assert blueprint_1.name == "Hello" assert blueprint_1.placeholders == {"test-placeholder"}
[ "def", "test_blueprint_properties", "(", "blueprint_1", ")", ":", "assert", "blueprint_1", ".", "metadata", "==", "{", "\"name\"", ":", "\"Hello\"", ",", "\"domain\"", ":", "\"automation\"", ",", "\"source_url\"", ":", "\"https://github.com/balloob/home-assistant-config/blob/main/blueprints/automation/motion_light.yaml\"", ",", "\"input\"", ":", "{", "\"test-placeholder\"", ":", "{", "\"name\"", ":", "\"Name\"", ",", "\"description\"", ":", "\"Description\"", "}", "}", ",", "}", "assert", "blueprint_1", ".", "domain", "==", "\"automation\"", "assert", "blueprint_1", ".", "name", "==", "\"Hello\"", "assert", "blueprint_1", ".", "placeholders", "==", "{", "\"test-placeholder\"", "}" ]
[ 59, 0 ]
[ 69, 59 ]
python
en
['en', 'nl', 'en']
False
test_blueprint_update_metadata
()
Test update metadata.
Test update metadata.
def test_blueprint_update_metadata(): """Test update metadata.""" bp = models.Blueprint( { "blueprint": { "name": "Hello", "domain": "automation", }, } ) bp.update_metadata(source_url="http://bla.com") assert bp.metadata["source_url"] == "http://bla.com"
[ "def", "test_blueprint_update_metadata", "(", ")", ":", "bp", "=", "models", ".", "Blueprint", "(", "{", "\"blueprint\"", ":", "{", "\"name\"", ":", "\"Hello\"", ",", "\"domain\"", ":", "\"automation\"", ",", "}", ",", "}", ")", "bp", ".", "update_metadata", "(", "source_url", "=", "\"http://bla.com\"", ")", "assert", "bp", ".", "metadata", "[", "\"source_url\"", "]", "==", "\"http://bla.com\"" ]
[ 72, 0 ]
[ 84, 56 ]
python
de
['en', 'de', 'it']
False
test_blueprint_validate
()
Test validate blueprint.
Test validate blueprint.
def test_blueprint_validate(): """Test validate blueprint.""" assert ( models.Blueprint( { "blueprint": { "name": "Hello", "domain": "automation", }, } ).validate() is None ) assert ( models.Blueprint( { "blueprint": { "name": "Hello", "domain": "automation", "homeassistant": {"min_version": "100000.0.0"}, }, } ).validate() == ["Requires at least Home Assistant 100000.0.0"] )
[ "def", "test_blueprint_validate", "(", ")", ":", "assert", "(", "models", ".", "Blueprint", "(", "{", "\"blueprint\"", ":", "{", "\"name\"", ":", "\"Hello\"", ",", "\"domain\"", ":", "\"automation\"", ",", "}", ",", "}", ")", ".", "validate", "(", ")", "is", "None", ")", "assert", "(", "models", ".", "Blueprint", "(", "{", "\"blueprint\"", ":", "{", "\"name\"", ":", "\"Hello\"", ",", "\"domain\"", ":", "\"automation\"", ",", "\"homeassistant\"", ":", "{", "\"min_version\"", ":", "\"100000.0.0\"", "}", ",", "}", ",", "}", ")", ".", "validate", "(", ")", "==", "[", "\"Requires at least Home Assistant 100000.0.0\"", "]", ")" ]
[ 87, 0 ]
[ 112, 5 ]
python
en
['en', 'et', 'it']
False
test_blueprint_inputs
(blueprint_1)
Test blueprint inputs.
Test blueprint inputs.
def test_blueprint_inputs(blueprint_1): """Test blueprint inputs.""" inputs = models.BlueprintInputs( blueprint_1, {"use_blueprint": {"path": "bla", "input": {"test-placeholder": 1}}}, ) inputs.validate() assert inputs.inputs == {"test-placeholder": 1} assert inputs.async_substitute() == {"example": 1}
[ "def", "test_blueprint_inputs", "(", "blueprint_1", ")", ":", "inputs", "=", "models", ".", "BlueprintInputs", "(", "blueprint_1", ",", "{", "\"use_blueprint\"", ":", "{", "\"path\"", ":", "\"bla\"", ",", "\"input\"", ":", "{", "\"test-placeholder\"", ":", "1", "}", "}", "}", ",", ")", "inputs", ".", "validate", "(", ")", "assert", "inputs", ".", "inputs", "==", "{", "\"test-placeholder\"", ":", "1", "}", "assert", "inputs", ".", "async_substitute", "(", ")", "==", "{", "\"example\"", ":", "1", "}" ]
[ 115, 0 ]
[ 123, 54 ]
python
en
['en', 'lb', 'en']
True
test_blueprint_inputs_validation
(blueprint_1)
Test blueprint input validation.
Test blueprint input validation.
def test_blueprint_inputs_validation(blueprint_1): """Test blueprint input validation.""" inputs = models.BlueprintInputs( blueprint_1, {"use_blueprint": {"path": "bla", "input": {"non-existing-placeholder": 1}}}, ) with pytest.raises(errors.MissingPlaceholder): inputs.validate()
[ "def", "test_blueprint_inputs_validation", "(", "blueprint_1", ")", ":", "inputs", "=", "models", ".", "BlueprintInputs", "(", "blueprint_1", ",", "{", "\"use_blueprint\"", ":", "{", "\"path\"", ":", "\"bla\"", ",", "\"input\"", ":", "{", "\"non-existing-placeholder\"", ":", "1", "}", "}", "}", ",", ")", "with", "pytest", ".", "raises", "(", "errors", ".", "MissingPlaceholder", ")", ":", "inputs", ".", "validate", "(", ")" ]
[ 126, 0 ]
[ 133, 25 ]
python
en
['en', 'et', 'en']
True
test_domain_blueprints_get_blueprint_errors
(hass, domain_bps)
Test domain blueprints.
Test domain blueprints.
async def test_domain_blueprints_get_blueprint_errors(hass, domain_bps): """Test domain blueprints.""" assert hass.data["blueprint"]["automation"] is domain_bps with pytest.raises(errors.FailedToLoad), patch( "homeassistant.util.yaml.load_yaml", side_effect=FileNotFoundError ): await domain_bps.async_get_blueprint("non-existing-path") with patch( "homeassistant.util.yaml.load_yaml", return_value={"blueprint": "invalid"} ): assert await domain_bps.async_get_blueprint("non-existing-path") is None
[ "async", "def", "test_domain_blueprints_get_blueprint_errors", "(", "hass", ",", "domain_bps", ")", ":", "assert", "hass", ".", "data", "[", "\"blueprint\"", "]", "[", "\"automation\"", "]", "is", "domain_bps", "with", "pytest", ".", "raises", "(", "errors", ".", "FailedToLoad", ")", ",", "patch", "(", "\"homeassistant.util.yaml.load_yaml\"", ",", "side_effect", "=", "FileNotFoundError", ")", ":", "await", "domain_bps", ".", "async_get_blueprint", "(", "\"non-existing-path\"", ")", "with", "patch", "(", "\"homeassistant.util.yaml.load_yaml\"", ",", "return_value", "=", "{", "\"blueprint\"", ":", "\"invalid\"", "}", ")", ":", "assert", "await", "domain_bps", ".", "async_get_blueprint", "(", "\"non-existing-path\"", ")", "is", "None" ]
[ 136, 0 ]
[ 148, 80 ]
python
fr
['fr', 'fr', 'en']
True
test_domain_blueprints_caching
(domain_bps)
Test domain blueprints cache blueprints.
Test domain blueprints cache blueprints.
async def test_domain_blueprints_caching(domain_bps): """Test domain blueprints cache blueprints.""" obj = object() with patch.object(domain_bps, "_load_blueprint", return_value=obj): assert await domain_bps.async_get_blueprint("something") is obj # Now we hit cache assert await domain_bps.async_get_blueprint("something") is obj obj_2 = object() domain_bps.async_reset_cache() # Now we call this method again. with patch.object(domain_bps, "_load_blueprint", return_value=obj_2): assert await domain_bps.async_get_blueprint("something") is obj_2
[ "async", "def", "test_domain_blueprints_caching", "(", "domain_bps", ")", ":", "obj", "=", "object", "(", ")", "with", "patch", ".", "object", "(", "domain_bps", ",", "\"_load_blueprint\"", ",", "return_value", "=", "obj", ")", ":", "assert", "await", "domain_bps", ".", "async_get_blueprint", "(", "\"something\"", ")", "is", "obj", "# Now we hit cache", "assert", "await", "domain_bps", ".", "async_get_blueprint", "(", "\"something\"", ")", "is", "obj", "obj_2", "=", "object", "(", ")", "domain_bps", ".", "async_reset_cache", "(", ")", "# Now we call this method again.", "with", "patch", ".", "object", "(", "domain_bps", ",", "\"_load_blueprint\"", ",", "return_value", "=", "obj_2", ")", ":", "assert", "await", "domain_bps", ".", "async_get_blueprint", "(", "\"something\"", ")", "is", "obj_2" ]
[ 151, 0 ]
[ 165, 73 ]
python
en
['en', 'fr', 'en']
True
test_domain_blueprints_inputs_from_config
(domain_bps, blueprint_1)
Test DomainBlueprints.async_inputs_from_config.
Test DomainBlueprints.async_inputs_from_config.
async def test_domain_blueprints_inputs_from_config(domain_bps, blueprint_1): """Test DomainBlueprints.async_inputs_from_config.""" with pytest.raises(errors.InvalidBlueprintInputs): await domain_bps.async_inputs_from_config({"not-referencing": "use_blueprint"}) with pytest.raises(errors.MissingPlaceholder), patch.object( domain_bps, "async_get_blueprint", return_value=blueprint_1 ): await domain_bps.async_inputs_from_config( {"use_blueprint": {"path": "bla.yaml", "input": {}}} ) with patch.object(domain_bps, "async_get_blueprint", return_value=blueprint_1): inputs = await domain_bps.async_inputs_from_config( {"use_blueprint": {"path": "bla.yaml", "input": {"test-placeholder": None}}} ) assert inputs.blueprint is blueprint_1 assert inputs.inputs == {"test-placeholder": None}
[ "async", "def", "test_domain_blueprints_inputs_from_config", "(", "domain_bps", ",", "blueprint_1", ")", ":", "with", "pytest", ".", "raises", "(", "errors", ".", "InvalidBlueprintInputs", ")", ":", "await", "domain_bps", ".", "async_inputs_from_config", "(", "{", "\"not-referencing\"", ":", "\"use_blueprint\"", "}", ")", "with", "pytest", ".", "raises", "(", "errors", ".", "MissingPlaceholder", ")", ",", "patch", ".", "object", "(", "domain_bps", ",", "\"async_get_blueprint\"", ",", "return_value", "=", "blueprint_1", ")", ":", "await", "domain_bps", ".", "async_inputs_from_config", "(", "{", "\"use_blueprint\"", ":", "{", "\"path\"", ":", "\"bla.yaml\"", ",", "\"input\"", ":", "{", "}", "}", "}", ")", "with", "patch", ".", "object", "(", "domain_bps", ",", "\"async_get_blueprint\"", ",", "return_value", "=", "blueprint_1", ")", ":", "inputs", "=", "await", "domain_bps", ".", "async_inputs_from_config", "(", "{", "\"use_blueprint\"", ":", "{", "\"path\"", ":", "\"bla.yaml\"", ",", "\"input\"", ":", "{", "\"test-placeholder\"", ":", "None", "}", "}", "}", ")", "assert", "inputs", ".", "blueprint", "is", "blueprint_1", "assert", "inputs", ".", "inputs", "==", "{", "\"test-placeholder\"", ":", "None", "}" ]
[ 168, 0 ]
[ 185, 54 ]
python
en
['en', 'en', 'en']
False
test_domain_blueprints_add_blueprint
(domain_bps, blueprint_1)
Test DomainBlueprints.async_add_blueprint.
Test DomainBlueprints.async_add_blueprint.
async def test_domain_blueprints_add_blueprint(domain_bps, blueprint_1): """Test DomainBlueprints.async_add_blueprint.""" with patch.object(domain_bps, "_create_file") as create_file_mock: # Should add extension when not present. await domain_bps.async_add_blueprint(blueprint_1, "something") assert create_file_mock.call_args[0][1] == ("something.yaml") await domain_bps.async_add_blueprint(blueprint_1, "something2.yaml") assert create_file_mock.call_args[0][1] == ("something2.yaml") # Should be in cache. with patch.object(domain_bps, "_load_blueprint") as mock_load: assert await domain_bps.async_get_blueprint("something.yaml") == blueprint_1 assert not mock_load.mock_calls
[ "async", "def", "test_domain_blueprints_add_blueprint", "(", "domain_bps", ",", "blueprint_1", ")", ":", "with", "patch", ".", "object", "(", "domain_bps", ",", "\"_create_file\"", ")", "as", "create_file_mock", ":", "# Should add extension when not present.", "await", "domain_bps", ".", "async_add_blueprint", "(", "blueprint_1", ",", "\"something\"", ")", "assert", "create_file_mock", ".", "call_args", "[", "0", "]", "[", "1", "]", "==", "(", "\"something.yaml\"", ")", "await", "domain_bps", ".", "async_add_blueprint", "(", "blueprint_1", ",", "\"something2.yaml\"", ")", "assert", "create_file_mock", ".", "call_args", "[", "0", "]", "[", "1", "]", "==", "(", "\"something2.yaml\"", ")", "# Should be in cache.", "with", "patch", ".", "object", "(", "domain_bps", ",", "\"_load_blueprint\"", ")", "as", "mock_load", ":", "assert", "await", "domain_bps", ".", "async_get_blueprint", "(", "\"something.yaml\"", ")", "==", "blueprint_1", "assert", "not", "mock_load", ".", "mock_calls" ]
[ 188, 0 ]
[ 201, 39 ]
python
en
['fr', 'hu', 'en']
False
create_matcher
(utterance)
Create a regex that matches the utterance.
Create a regex that matches the utterance.
def create_matcher(utterance): """Create a regex that matches the utterance.""" # Split utterance into parts that are type: NORMAL, GROUP or OPTIONAL # Pattern matches (GROUP|OPTIONAL): Change light to [the color] {name} parts = re.split(r"({\w+}|\[[\w\s]+\] *)", utterance) # Pattern to extract name from GROUP part. Matches {name} group_matcher = re.compile(r"{(\w+)}") # Pattern to extract text from OPTIONAL part. Matches [the color] optional_matcher = re.compile(r"\[([\w ]+)\] *") pattern = ["^"] for part in parts: group_match = group_matcher.match(part) optional_match = optional_matcher.match(part) # Normal part if group_match is None and optional_match is None: pattern.append(part) continue # Group part if group_match is not None: pattern.append(r"(?P<{}>[\w ]+?)\s*".format(group_match.groups()[0])) # Optional part elif optional_match is not None: pattern.append(r"(?:{} *)?".format(optional_match.groups()[0])) pattern.append("$") return re.compile("".join(pattern), re.I)
[ "def", "create_matcher", "(", "utterance", ")", ":", "# Split utterance into parts that are type: NORMAL, GROUP or OPTIONAL", "# Pattern matches (GROUP|OPTIONAL): Change light to [the color] {name}", "parts", "=", "re", ".", "split", "(", "r\"({\\w+}|\\[[\\w\\s]+\\] *)\"", ",", "utterance", ")", "# Pattern to extract name from GROUP part. Matches {name}", "group_matcher", "=", "re", ".", "compile", "(", "r\"{(\\w+)}\"", ")", "# Pattern to extract text from OPTIONAL part. Matches [the color]", "optional_matcher", "=", "re", ".", "compile", "(", "r\"\\[([\\w ]+)\\] *\"", ")", "pattern", "=", "[", "\"^\"", "]", "for", "part", "in", "parts", ":", "group_match", "=", "group_matcher", ".", "match", "(", "part", ")", "optional_match", "=", "optional_matcher", ".", "match", "(", "part", ")", "# Normal part", "if", "group_match", "is", "None", "and", "optional_match", "is", "None", ":", "pattern", ".", "append", "(", "part", ")", "continue", "# Group part", "if", "group_match", "is", "not", "None", ":", "pattern", ".", "append", "(", "r\"(?P<{}>[\\w ]+?)\\s*\"", ".", "format", "(", "group_match", ".", "groups", "(", ")", "[", "0", "]", ")", ")", "# Optional part", "elif", "optional_match", "is", "not", "None", ":", "pattern", ".", "append", "(", "r\"(?:{} *)?\"", ".", "format", "(", "optional_match", ".", "groups", "(", ")", "[", "0", "]", ")", ")", "pattern", ".", "append", "(", "\"$\"", ")", "return", "re", ".", "compile", "(", "\"\"", ".", "join", "(", "pattern", ")", ",", "re", ".", "I", ")" ]
[ 4, 0 ]
[ 33, 45 ]
python
en
['en', 'en', 'en']
True
test_form
(hass)
Test we get the form.
Test we get the form.
async def test_form(hass): """Test we get the form.""" await setup.async_setup_component(hass, "persistent_notification", {}) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) assert result["type"] == "form" assert result["errors"] == {} with patch("smart_meter_texas.Client.authenticate", return_value=True), patch( "homeassistant.components.smart_meter_texas.async_setup", return_value=True ) as mock_setup, patch( "homeassistant.components.smart_meter_texas.async_setup_entry", return_value=True, ) as mock_setup_entry: result2 = await hass.config_entries.flow.async_configure( result["flow_id"], TEST_LOGIN ) await hass.async_block_till_done() assert result2["type"] == "create_entry" assert result2["title"] == TEST_LOGIN[CONF_USERNAME] assert result2["data"] == TEST_LOGIN assert len(mock_setup.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
[ "async", "def", "test_form", "(", "hass", ")", ":", "await", "setup", ".", "async_setup_component", "(", "hass", ",", "\"persistent_notification\"", ",", "{", "}", ")", "result", "=", "await", "hass", ".", "config_entries", ".", "flow", ".", "async_init", "(", "DOMAIN", ",", "context", "=", "{", "\"source\"", ":", "config_entries", ".", "SOURCE_USER", "}", ")", "assert", "result", "[", "\"type\"", "]", "==", "\"form\"", "assert", "result", "[", "\"errors\"", "]", "==", "{", "}", "with", "patch", "(", "\"smart_meter_texas.Client.authenticate\"", ",", "return_value", "=", "True", ")", ",", "patch", "(", "\"homeassistant.components.smart_meter_texas.async_setup\"", ",", "return_value", "=", "True", ")", "as", "mock_setup", ",", "patch", "(", "\"homeassistant.components.smart_meter_texas.async_setup_entry\"", ",", "return_value", "=", "True", ",", ")", "as", "mock_setup_entry", ":", "result2", "=", "await", "hass", ".", "config_entries", ".", "flow", ".", "async_configure", "(", "result", "[", "\"flow_id\"", "]", ",", "TEST_LOGIN", ")", "await", "hass", ".", "async_block_till_done", "(", ")", "assert", "result2", "[", "\"type\"", "]", "==", "\"create_entry\"", "assert", "result2", "[", "\"title\"", "]", "==", "TEST_LOGIN", "[", "CONF_USERNAME", "]", "assert", "result2", "[", "\"data\"", "]", "==", "TEST_LOGIN", "assert", "len", "(", "mock_setup", ".", "mock_calls", ")", "==", "1", "assert", "len", "(", "mock_setup_entry", ".", "mock_calls", ")", "==", "1" ]
[ 20, 0 ]
[ 44, 48 ]
python
en
['en', 'en', 'en']
True
test_form_invalid_auth
(hass)
Test we handle invalid auth.
Test we handle invalid auth.
async def test_form_invalid_auth(hass): """Test we handle invalid auth.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) with patch( "smart_meter_texas.Client.authenticate", side_effect=SmartMeterTexasAuthError, ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], TEST_LOGIN, ) assert result2["type"] == "form" assert result2["errors"] == {"base": "invalid_auth"}
[ "async", "def", "test_form_invalid_auth", "(", "hass", ")", ":", "result", "=", "await", "hass", ".", "config_entries", ".", "flow", ".", "async_init", "(", "DOMAIN", ",", "context", "=", "{", "\"source\"", ":", "config_entries", ".", "SOURCE_USER", "}", ")", "with", "patch", "(", "\"smart_meter_texas.Client.authenticate\"", ",", "side_effect", "=", "SmartMeterTexasAuthError", ",", ")", ":", "result2", "=", "await", "hass", ".", "config_entries", ".", "flow", ".", "async_configure", "(", "result", "[", "\"flow_id\"", "]", ",", "TEST_LOGIN", ",", ")", "assert", "result2", "[", "\"type\"", "]", "==", "\"form\"", "assert", "result2", "[", "\"errors\"", "]", "==", "{", "\"base\"", ":", "\"invalid_auth\"", "}" ]
[ 47, 0 ]
[ 63, 56 ]
python
en
['en', 'en', 'en']
True
test_form_cannot_connect
(hass, side_effect)
Test we handle cannot connect error.
Test we handle cannot connect error.
async def test_form_cannot_connect(hass, side_effect): """Test we handle cannot connect error.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) with patch( "smart_meter_texas.Client.authenticate", side_effect=side_effect, ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], TEST_LOGIN ) assert result2["type"] == "form" assert result2["errors"] == {"base": "cannot_connect"}
[ "async", "def", "test_form_cannot_connect", "(", "hass", ",", "side_effect", ")", ":", "result", "=", "await", "hass", ".", "config_entries", ".", "flow", ".", "async_init", "(", "DOMAIN", ",", "context", "=", "{", "\"source\"", ":", "config_entries", ".", "SOURCE_USER", "}", ")", "with", "patch", "(", "\"smart_meter_texas.Client.authenticate\"", ",", "side_effect", "=", "side_effect", ",", ")", ":", "result2", "=", "await", "hass", ".", "config_entries", ".", "flow", ".", "async_configure", "(", "result", "[", "\"flow_id\"", "]", ",", "TEST_LOGIN", ")", "assert", "result2", "[", "\"type\"", "]", "==", "\"form\"", "assert", "result2", "[", "\"errors\"", "]", "==", "{", "\"base\"", ":", "\"cannot_connect\"", "}" ]
[ 69, 0 ]
[ 84, 58 ]
python
en
['en', 'en', 'en']
True
test_form_unknown_exception
(hass)
Test base exception is handled.
Test base exception is handled.
async def test_form_unknown_exception(hass): """Test base exception is handled.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) with patch( "smart_meter_texas.Client.authenticate", side_effect=Exception, ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], TEST_LOGIN, ) assert result2["type"] == "form" assert result2["errors"] == {"base": "unknown"}
[ "async", "def", "test_form_unknown_exception", "(", "hass", ")", ":", "result", "=", "await", "hass", ".", "config_entries", ".", "flow", ".", "async_init", "(", "DOMAIN", ",", "context", "=", "{", "\"source\"", ":", "config_entries", ".", "SOURCE_USER", "}", ")", "with", "patch", "(", "\"smart_meter_texas.Client.authenticate\"", ",", "side_effect", "=", "Exception", ",", ")", ":", "result2", "=", "await", "hass", ".", "config_entries", ".", "flow", ".", "async_configure", "(", "result", "[", "\"flow_id\"", "]", ",", "TEST_LOGIN", ",", ")", "assert", "result2", "[", "\"type\"", "]", "==", "\"form\"", "assert", "result2", "[", "\"errors\"", "]", "==", "{", "\"base\"", ":", "\"unknown\"", "}" ]
[ 87, 0 ]
[ 103, 51 ]
python
en
['en', 'en', 'en']
True
test_form_duplicate_account
(hass)
Test that a duplicate account cannot be configured.
Test that a duplicate account cannot be configured.
async def test_form_duplicate_account(hass): """Test that a duplicate account cannot be configured.""" MockConfigEntry( domain=DOMAIN, unique_id="user123", data={"username": "user123", "password": "password123"}, ).add_to_hass(hass) with patch( "smart_meter_texas.Client.authenticate", return_value=True, ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}, data={"username": "user123", "password": "password123"}, ) assert result["type"] == "abort" assert result["reason"] == "already_configured"
[ "async", "def", "test_form_duplicate_account", "(", "hass", ")", ":", "MockConfigEntry", "(", "domain", "=", "DOMAIN", ",", "unique_id", "=", "\"user123\"", ",", "data", "=", "{", "\"username\"", ":", "\"user123\"", ",", "\"password\"", ":", "\"password123\"", "}", ",", ")", ".", "add_to_hass", "(", "hass", ")", "with", "patch", "(", "\"smart_meter_texas.Client.authenticate\"", ",", "return_value", "=", "True", ",", ")", ":", "result", "=", "await", "hass", ".", "config_entries", ".", "flow", ".", "async_init", "(", "DOMAIN", ",", "context", "=", "{", "\"source\"", ":", "config_entries", ".", "SOURCE_USER", "}", ",", "data", "=", "{", "\"username\"", ":", "\"user123\"", ",", "\"password\"", ":", "\"password123\"", "}", ",", ")", "assert", "result", "[", "\"type\"", "]", "==", "\"abort\"", "assert", "result", "[", "\"reason\"", "]", "==", "\"already_configured\"" ]
[ 106, 0 ]
[ 125, 51 ]
python
en
['en', 'en', 'en']
True
async_setup_platform
(hass, config, async_add_entities, discovery_info=None)
Set up the IMAP platform.
Set up the IMAP platform.
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the IMAP platform.""" sensor = ImapSensor( config.get(CONF_NAME), config.get(CONF_USERNAME), config.get(CONF_PASSWORD), config.get(CONF_SERVER), config.get(CONF_PORT), config.get(CONF_CHARSET), config.get(CONF_FOLDER), config.get(CONF_SEARCH), ) if not await sensor.connection(): raise PlatformNotReady hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, sensor.shutdown) async_add_entities([sensor], True)
[ "async", "def", "async_setup_platform", "(", "hass", ",", "config", ",", "async_add_entities", ",", "discovery_info", "=", "None", ")", ":", "sensor", "=", "ImapSensor", "(", "config", ".", "get", "(", "CONF_NAME", ")", ",", "config", ".", "get", "(", "CONF_USERNAME", ")", ",", "config", ".", "get", "(", "CONF_PASSWORD", ")", ",", "config", ".", "get", "(", "CONF_SERVER", ")", ",", "config", ".", "get", "(", "CONF_PORT", ")", ",", "config", ".", "get", "(", "CONF_CHARSET", ")", ",", "config", ".", "get", "(", "CONF_FOLDER", ")", ",", "config", ".", "get", "(", "CONF_SEARCH", ")", ",", ")", "if", "not", "await", "sensor", ".", "connection", "(", ")", ":", "raise", "PlatformNotReady", "hass", ".", "bus", ".", "async_listen_once", "(", "EVENT_HOMEASSISTANT_STOP", ",", "sensor", ".", "shutdown", ")", "async_add_entities", "(", "[", "sensor", "]", ",", "True", ")" ]
[ 45, 0 ]
[ 61, 38 ]
python
en
['en', 'ru', 'en']
True
ImapSensor.__init__
(self, name, user, password, server, port, charset, folder, search)
Initialize the sensor.
Initialize the sensor.
def __init__(self, name, user, password, server, port, charset, folder, search): """Initialize the sensor.""" self._name = name or user self._user = user self._password = password self._server = server self._port = port self._charset = charset self._folder = folder self._email_count = None self._search = search self._connection = None self._does_push = None self._idle_loop_task = None
[ "def", "__init__", "(", "self", ",", "name", ",", "user", ",", "password", ",", "server", ",", "port", ",", "charset", ",", "folder", ",", "search", ")", ":", "self", ".", "_name", "=", "name", "or", "user", "self", ".", "_user", "=", "user", "self", ".", "_password", "=", "password", "self", ".", "_server", "=", "server", "self", ".", "_port", "=", "port", "self", ".", "_charset", "=", "charset", "self", ".", "_folder", "=", "folder", "self", ".", "_email_count", "=", "None", "self", ".", "_search", "=", "search", "self", ".", "_connection", "=", "None", "self", ".", "_does_push", "=", "None", "self", ".", "_idle_loop_task", "=", "None" ]
[ 67, 4 ]
[ 80, 35 ]
python
en
['en', 'en', 'en']
True
ImapSensor.async_added_to_hass
(self)
Handle when an entity is about to be added to Home Assistant.
Handle when an entity is about to be added to Home Assistant.
async def async_added_to_hass(self): """Handle when an entity is about to be added to Home Assistant.""" if not self.should_poll: self._idle_loop_task = self.hass.loop.create_task(self.idle_loop())
[ "async", "def", "async_added_to_hass", "(", "self", ")", ":", "if", "not", "self", ".", "should_poll", ":", "self", ".", "_idle_loop_task", "=", "self", ".", "hass", ".", "loop", ".", "create_task", "(", "self", ".", "idle_loop", "(", ")", ")" ]
[ 82, 4 ]
[ 85, 79 ]
python
en
['en', 'en', 'en']
True
ImapSensor.name
(self)
Return the name of the sensor.
Return the name of the sensor.
def name(self): """Return the name of the sensor.""" return self._name
[ "def", "name", "(", "self", ")", ":", "return", "self", ".", "_name" ]
[ 88, 4 ]
[ 90, 25 ]
python
en
['en', 'mi', 'en']
True
ImapSensor.icon
(self)
Return the icon to use in the frontend.
Return the icon to use in the frontend.
def icon(self): """Return the icon to use in the frontend.""" return ICON
[ "def", "icon", "(", "self", ")", ":", "return", "ICON" ]
[ 93, 4 ]
[ 95, 19 ]
python
en
['en', 'en', 'en']
True
ImapSensor.state
(self)
Return the number of emails found.
Return the number of emails found.
def state(self): """Return the number of emails found.""" return self._email_count
[ "def", "state", "(", "self", ")", ":", "return", "self", ".", "_email_count" ]
[ 98, 4 ]
[ 100, 32 ]
python
en
['en', 'en', 'en']
True
ImapSensor.available
(self)
Return the availability of the device.
Return the availability of the device.
def available(self): """Return the availability of the device.""" return self._connection is not None
[ "def", "available", "(", "self", ")", ":", "return", "self", ".", "_connection", "is", "not", "None" ]
[ 103, 4 ]
[ 105, 43 ]
python
en
['en', 'en', 'en']
True
ImapSensor.should_poll
(self)
Return if polling is needed.
Return if polling is needed.
def should_poll(self): """Return if polling is needed.""" return not self._does_push
[ "def", "should_poll", "(", "self", ")", ":", "return", "not", "self", ".", "_does_push" ]
[ 108, 4 ]
[ 110, 34 ]
python
en
['en', 'en', 'en']
True
ImapSensor.connection
(self)
Return a connection to the server, establishing it if necessary.
Return a connection to the server, establishing it if necessary.
async def connection(self): """Return a connection to the server, establishing it if necessary.""" if self._connection is None: try: self._connection = IMAP4_SSL(self._server, self._port) await self._connection.wait_hello_from_server() await self._connection.login(self._user, self._password) await self._connection.select(self._folder) self._does_push = self._connection.has_capability("IDLE") except (AioImapException, asyncio.TimeoutError): self._connection = None return self._connection
[ "async", "def", "connection", "(", "self", ")", ":", "if", "self", ".", "_connection", "is", "None", ":", "try", ":", "self", ".", "_connection", "=", "IMAP4_SSL", "(", "self", ".", "_server", ",", "self", ".", "_port", ")", "await", "self", ".", "_connection", ".", "wait_hello_from_server", "(", ")", "await", "self", ".", "_connection", ".", "login", "(", "self", ".", "_user", ",", "self", ".", "_password", ")", "await", "self", ".", "_connection", ".", "select", "(", "self", ".", "_folder", ")", "self", ".", "_does_push", "=", "self", ".", "_connection", ".", "has_capability", "(", "\"IDLE\"", ")", "except", "(", "AioImapException", ",", "asyncio", ".", "TimeoutError", ")", ":", "self", ".", "_connection", "=", "None", "return", "self", ".", "_connection" ]
[ 112, 4 ]
[ 124, 31 ]
python
en
['en', 'en', 'en']
True
ImapSensor.idle_loop
(self)
Wait for data pushed from server.
Wait for data pushed from server.
async def idle_loop(self): """Wait for data pushed from server.""" while True: try: if await self.connection(): await self.refresh_email_count() self.async_write_ha_state() idle = await self._connection.idle_start() await self._connection.wait_server_push() self._connection.idle_done() with async_timeout.timeout(10): await idle else: self.async_write_ha_state() except (AioImapException, asyncio.TimeoutError): self.disconnected()
[ "async", "def", "idle_loop", "(", "self", ")", ":", "while", "True", ":", "try", ":", "if", "await", "self", ".", "connection", "(", ")", ":", "await", "self", ".", "refresh_email_count", "(", ")", "self", ".", "async_write_ha_state", "(", ")", "idle", "=", "await", "self", ".", "_connection", ".", "idle_start", "(", ")", "await", "self", ".", "_connection", ".", "wait_server_push", "(", ")", "self", ".", "_connection", ".", "idle_done", "(", ")", "with", "async_timeout", ".", "timeout", "(", "10", ")", ":", "await", "idle", "else", ":", "self", ".", "async_write_ha_state", "(", ")", "except", "(", "AioImapException", ",", "asyncio", ".", "TimeoutError", ")", ":", "self", ".", "disconnected", "(", ")" ]
[ 126, 4 ]
[ 142, 35 ]
python
en
['en', 'en', 'en']
True
ImapSensor.async_update
(self)
Periodic polling of state.
Periodic polling of state.
async def async_update(self): """Periodic polling of state.""" try: if await self.connection(): await self.refresh_email_count() except (AioImapException, asyncio.TimeoutError): self.disconnected()
[ "async", "def", "async_update", "(", "self", ")", ":", "try", ":", "if", "await", "self", ".", "connection", "(", ")", ":", "await", "self", ".", "refresh_email_count", "(", ")", "except", "(", "AioImapException", ",", "asyncio", ".", "TimeoutError", ")", ":", "self", ".", "disconnected", "(", ")" ]
[ 144, 4 ]
[ 150, 31 ]
python
en
['en', 'en', 'en']
True
ImapSensor.refresh_email_count
(self)
Check the number of found emails.
Check the number of found emails.
async def refresh_email_count(self): """Check the number of found emails.""" if self._connection: await self._connection.noop() result, lines = await self._connection.search( self._search, charset=self._charset ) if result == "OK": self._email_count = len(lines[0].split()) else: _LOGGER.error( "Can't parse IMAP server response to search '%s': %s / %s", self._search, result, lines[0], )
[ "async", "def", "refresh_email_count", "(", "self", ")", ":", "if", "self", ".", "_connection", ":", "await", "self", ".", "_connection", ".", "noop", "(", ")", "result", ",", "lines", "=", "await", "self", ".", "_connection", ".", "search", "(", "self", ".", "_search", ",", "charset", "=", "self", ".", "_charset", ")", "if", "result", "==", "\"OK\"", ":", "self", ".", "_email_count", "=", "len", "(", "lines", "[", "0", "]", ".", "split", "(", ")", ")", "else", ":", "_LOGGER", ".", "error", "(", "\"Can't parse IMAP server response to search '%s': %s / %s\"", ",", "self", ".", "_search", ",", "result", ",", "lines", "[", "0", "]", ",", ")" ]
[ 152, 4 ]
[ 168, 17 ]
python
en
['en', 'en', 'en']
True
ImapSensor.disconnected
(self)
Forget the connection after it was lost.
Forget the connection after it was lost.
def disconnected(self): """Forget the connection after it was lost.""" _LOGGER.warning("Lost %s (will attempt to reconnect)", self._server) self._connection = None
[ "def", "disconnected", "(", "self", ")", ":", "_LOGGER", ".", "warning", "(", "\"Lost %s (will attempt to reconnect)\"", ",", "self", ".", "_server", ")", "self", ".", "_connection", "=", "None" ]
[ 170, 4 ]
[ 173, 31 ]
python
en
['en', 'en', 'en']
True
ImapSensor.shutdown
(self, *_)
Close resources.
Close resources.
async def shutdown(self, *_): """Close resources.""" if self._connection: if self._connection.has_pending_idle(): self._connection.idle_done() await self._connection.logout() if self._idle_loop_task: self._idle_loop_task.cancel()
[ "async", "def", "shutdown", "(", "self", ",", "*", "_", ")", ":", "if", "self", ".", "_connection", ":", "if", "self", ".", "_connection", ".", "has_pending_idle", "(", ")", ":", "self", ".", "_connection", ".", "idle_done", "(", ")", "await", "self", ".", "_connection", ".", "logout", "(", ")", "if", "self", ".", "_idle_loop_task", ":", "self", ".", "_idle_loop_task", ".", "cancel", "(", ")" ]
[ 175, 4 ]
[ 182, 41 ]
python
en
['en', 'la', 'en']
False
registered_flow
(hass)
Mock a registered flow.
Mock a registered flow.
def registered_flow(hass): """Mock a registered flow.""" local_auth.initialize(hass, "TEST-CLIENT-ID", "TEST-CLIENT-SECRET") return hass.data[config_flow.DATA_FLOW_IMPL][const.DOMAIN]
[ "def", "registered_flow", "(", "hass", ")", ":", "local_auth", ".", "initialize", "(", "hass", ",", "\"TEST-CLIENT-ID\"", ",", "\"TEST-CLIENT-SECRET\"", ")", "return", "hass", ".", "data", "[", "config_flow", ".", "DATA_FLOW_IMPL", "]", "[", "const", ".", "DOMAIN", "]" ]
[ 10, 0 ]
[ 13, 62 ]
python
en
['en', 'da', 'en']
True
test_generate_auth_url
(registered_flow)
Test generating an auth url. Mainly testing that it doesn't blow up.
Test generating an auth url.
async def test_generate_auth_url(registered_flow): """Test generating an auth url. Mainly testing that it doesn't blow up. """ url = await registered_flow["gen_authorize_url"]("TEST-FLOW-ID") assert url is not None
[ "async", "def", "test_generate_auth_url", "(", "registered_flow", ")", ":", "url", "=", "await", "registered_flow", "[", "\"gen_authorize_url\"", "]", "(", "\"TEST-FLOW-ID\"", ")", "assert", "url", "is", "not", "None" ]
[ 16, 0 ]
[ 22, 26 ]
python
de
['de', 'en', 'it']
False
test_convert_code
(requests_mock, registered_flow)
Test converting a code.
Test converting a code.
async def test_convert_code(requests_mock, registered_flow): """Test converting a code.""" from nest.nest import ACCESS_TOKEN_URL def token_matcher(request): """Match a fetch token request.""" if request.url != ACCESS_TOKEN_URL: return None assert dict(parse_qsl(request.text)) == { "client_id": "TEST-CLIENT-ID", "client_secret": "TEST-CLIENT-SECRET", "code": "TEST-CODE", "grant_type": "authorization_code", } return rmock.create_response( request, json={"access_token": "TEST-ACCESS-TOKEN"} ) requests_mock.add_matcher(token_matcher) tokens = await registered_flow["convert_code"]("TEST-CODE") assert tokens == {"access_token": "TEST-ACCESS-TOKEN"}
[ "async", "def", "test_convert_code", "(", "requests_mock", ",", "registered_flow", ")", ":", "from", "nest", ".", "nest", "import", "ACCESS_TOKEN_URL", "def", "token_matcher", "(", "request", ")", ":", "\"\"\"Match a fetch token request.\"\"\"", "if", "request", ".", "url", "!=", "ACCESS_TOKEN_URL", ":", "return", "None", "assert", "dict", "(", "parse_qsl", "(", "request", ".", "text", ")", ")", "==", "{", "\"client_id\"", ":", "\"TEST-CLIENT-ID\"", ",", "\"client_secret\"", ":", "\"TEST-CLIENT-SECRET\"", ",", "\"code\"", ":", "\"TEST-CODE\"", ",", "\"grant_type\"", ":", "\"authorization_code\"", ",", "}", "return", "rmock", ".", "create_response", "(", "request", ",", "json", "=", "{", "\"access_token\"", ":", "\"TEST-ACCESS-TOKEN\"", "}", ")", "requests_mock", ".", "add_matcher", "(", "token_matcher", ")", "tokens", "=", "await", "registered_flow", "[", "\"convert_code\"", "]", "(", "\"TEST-CODE\"", ")", "assert", "tokens", "==", "{", "\"access_token\"", ":", "\"TEST-ACCESS-TOKEN\"", "}" ]
[ 25, 0 ]
[ 48, 58 ]
python
en
['en', 'en', 'en']
True
setup_platform
(hass, config, add_entities, discovery_info=None)
Set up a wake on lan switch.
Set up a wake on lan switch.
def setup_platform(hass, config, add_entities, discovery_info=None): """Set up a wake on lan switch.""" broadcast_address = config.get(CONF_BROADCAST_ADDRESS) broadcast_port = config.get(CONF_BROADCAST_PORT) host = config.get(CONF_HOST) mac_address = config[CONF_MAC] name = config[CONF_NAME] off_action = config.get(CONF_OFF_ACTION) add_entities( [ WolSwitch( hass, name, host, mac_address, off_action, broadcast_address, broadcast_port, ) ], True, )
[ "def", "setup_platform", "(", "hass", ",", "config", ",", "add_entities", ",", "discovery_info", "=", "None", ")", ":", "broadcast_address", "=", "config", ".", "get", "(", "CONF_BROADCAST_ADDRESS", ")", "broadcast_port", "=", "config", ".", "get", "(", "CONF_BROADCAST_PORT", ")", "host", "=", "config", ".", "get", "(", "CONF_HOST", ")", "mac_address", "=", "config", "[", "CONF_MAC", "]", "name", "=", "config", "[", "CONF_NAME", "]", "off_action", "=", "config", ".", "get", "(", "CONF_OFF_ACTION", ")", "add_entities", "(", "[", "WolSwitch", "(", "hass", ",", "name", ",", "host", ",", "mac_address", ",", "off_action", ",", "broadcast_address", ",", "broadcast_port", ",", ")", "]", ",", "True", ",", ")" ]
[ 38, 0 ]
[ 60, 5 ]
python
en
['en', 'jv', 'en']
True
WolSwitch.__init__
( self, hass, name, host, mac_address, off_action, broadcast_address, broadcast_port, )
Initialize the WOL switch.
Initialize the WOL switch.
def __init__( self, hass, name, host, mac_address, off_action, broadcast_address, broadcast_port, ): """Initialize the WOL switch.""" self._hass = hass self._name = name self._host = host self._mac_address = mac_address self._broadcast_address = broadcast_address self._broadcast_port = broadcast_port domain = __name__.split(".")[-2] self._off_script = ( Script(hass, off_action, name, domain) if off_action else None ) self._state = False
[ "def", "__init__", "(", "self", ",", "hass", ",", "name", ",", "host", ",", "mac_address", ",", "off_action", ",", "broadcast_address", ",", "broadcast_port", ",", ")", ":", "self", ".", "_hass", "=", "hass", "self", ".", "_name", "=", "name", "self", ".", "_host", "=", "host", "self", ".", "_mac_address", "=", "mac_address", "self", ".", "_broadcast_address", "=", "broadcast_address", "self", ".", "_broadcast_port", "=", "broadcast_port", "domain", "=", "__name__", ".", "split", "(", "\".\"", ")", "[", "-", "2", "]", "self", ".", "_off_script", "=", "(", "Script", "(", "hass", ",", "off_action", ",", "name", ",", "domain", ")", "if", "off_action", "else", "None", ")", "self", ".", "_state", "=", "False" ]
[ 66, 4 ]
[ 87, 27 ]
python
en
['en', 'en', 'en']
True
WolSwitch.is_on
(self)
Return true if switch is on.
Return true if switch is on.
def is_on(self): """Return true if switch is on.""" return self._state
[ "def", "is_on", "(", "self", ")", ":", "return", "self", ".", "_state" ]
[ 90, 4 ]
[ 92, 26 ]
python
en
['en', 'fy', 'en']
True
WolSwitch.name
(self)
Return the name of the switch.
Return the name of the switch.
def name(self): """Return the name of the switch.""" return self._name
[ "def", "name", "(", "self", ")", ":", "return", "self", ".", "_name" ]
[ 95, 4 ]
[ 97, 25 ]
python
en
['en', 'en', 'en']
True
WolSwitch.turn_on
(self, **kwargs)
Turn the device on.
Turn the device on.
def turn_on(self, **kwargs): """Turn the device on.""" service_kwargs = {} if self._broadcast_address is not None: service_kwargs["ip_address"] = self._broadcast_address if self._broadcast_port is not None: service_kwargs["port"] = self._broadcast_port _LOGGER.info( "Send magic packet to mac %s (broadcast: %s, port: %s)", self._mac_address, self._broadcast_address, self._broadcast_port, ) wakeonlan.send_magic_packet(self._mac_address, **service_kwargs)
[ "def", "turn_on", "(", "self", ",", "*", "*", "kwargs", ")", ":", "service_kwargs", "=", "{", "}", "if", "self", ".", "_broadcast_address", "is", "not", "None", ":", "service_kwargs", "[", "\"ip_address\"", "]", "=", "self", ".", "_broadcast_address", "if", "self", ".", "_broadcast_port", "is", "not", "None", ":", "service_kwargs", "[", "\"port\"", "]", "=", "self", ".", "_broadcast_port", "_LOGGER", ".", "info", "(", "\"Send magic packet to mac %s (broadcast: %s, port: %s)\"", ",", "self", ".", "_mac_address", ",", "self", ".", "_broadcast_address", ",", "self", ".", "_broadcast_port", ",", ")", "wakeonlan", ".", "send_magic_packet", "(", "self", ".", "_mac_address", ",", "*", "*", "service_kwargs", ")" ]
[ 99, 4 ]
[ 114, 72 ]
python
en
['en', 'en', 'en']
True
WolSwitch.turn_off
(self, **kwargs)
Turn the device off if an off action is present.
Turn the device off if an off action is present.
def turn_off(self, **kwargs): """Turn the device off if an off action is present.""" if self._off_script is not None: self._off_script.run(context=self._context)
[ "def", "turn_off", "(", "self", ",", "*", "*", "kwargs", ")", ":", "if", "self", ".", "_off_script", "is", "not", "None", ":", "self", ".", "_off_script", ".", "run", "(", "context", "=", "self", ".", "_context", ")" ]
[ 116, 4 ]
[ 119, 55 ]
python
en
['en', 'en', 'en']
True
WolSwitch.update
(self)
Check if device is on and update the state.
Check if device is on and update the state.
def update(self): """Check if device is on and update the state.""" if platform.system().lower() == "windows": ping_cmd = [ "ping", "-n", "1", "-w", str(DEFAULT_PING_TIMEOUT * 1000), str(self._host), ] else: ping_cmd = [ "ping", "-c", "1", "-W", str(DEFAULT_PING_TIMEOUT), str(self._host), ] status = sp.call(ping_cmd, stdout=sp.DEVNULL, stderr=sp.DEVNULL) self._state = not bool(status)
[ "def", "update", "(", "self", ")", ":", "if", "platform", ".", "system", "(", ")", ".", "lower", "(", ")", "==", "\"windows\"", ":", "ping_cmd", "=", "[", "\"ping\"", ",", "\"-n\"", ",", "\"1\"", ",", "\"-w\"", ",", "str", "(", "DEFAULT_PING_TIMEOUT", "*", "1000", ")", ",", "str", "(", "self", ".", "_host", ")", ",", "]", "else", ":", "ping_cmd", "=", "[", "\"ping\"", ",", "\"-c\"", ",", "\"1\"", ",", "\"-W\"", ",", "str", "(", "DEFAULT_PING_TIMEOUT", ")", ",", "str", "(", "self", ".", "_host", ")", ",", "]", "status", "=", "sp", ".", "call", "(", "ping_cmd", ",", "stdout", "=", "sp", ".", "DEVNULL", ",", "stderr", "=", "sp", ".", "DEVNULL", ")", "self", ".", "_state", "=", "not", "bool", "(", "status", ")" ]
[ 121, 4 ]
[ 143, 38 ]
python
en
['en', 'en', 'en']
True
async_setup_entry
(hass, config_entry, async_add_entities)
Perform the setup for Xiaomi devices.
Perform the setup for Xiaomi devices.
async def async_setup_entry(hass, config_entry, async_add_entities): """Perform the setup for Xiaomi devices.""" entities = [] gateway = hass.data[DOMAIN][GATEWAYS_KEY][config_entry.entry_id] for device in gateway.devices["light"]: model = device["model"] if model in ["gateway", "gateway.v3"]: entities.append( XiaomiGatewayLight(device, "Gateway Light", gateway, config_entry) ) async_add_entities(entities)
[ "async", "def", "async_setup_entry", "(", "hass", ",", "config_entry", ",", "async_add_entities", ")", ":", "entities", "=", "[", "]", "gateway", "=", "hass", ".", "data", "[", "DOMAIN", "]", "[", "GATEWAYS_KEY", "]", "[", "config_entry", ".", "entry_id", "]", "for", "device", "in", "gateway", ".", "devices", "[", "\"light\"", "]", ":", "model", "=", "device", "[", "\"model\"", "]", "if", "model", "in", "[", "\"gateway\"", ",", "\"gateway.v3\"", "]", ":", "entities", ".", "append", "(", "XiaomiGatewayLight", "(", "device", ",", "\"Gateway Light\"", ",", "gateway", ",", "config_entry", ")", ")", "async_add_entities", "(", "entities", ")" ]
[ 20, 0 ]
[ 30, 32 ]
python
en
['en', 'en', 'en']
True
XiaomiGatewayLight.__init__
(self, device, name, xiaomi_hub, config_entry)
Initialize the XiaomiGatewayLight.
Initialize the XiaomiGatewayLight.
def __init__(self, device, name, xiaomi_hub, config_entry): """Initialize the XiaomiGatewayLight.""" self._data_key = "rgb" self._hs = (0, 0) self._brightness = 100 super().__init__(device, name, xiaomi_hub, config_entry)
[ "def", "__init__", "(", "self", ",", "device", ",", "name", ",", "xiaomi_hub", ",", "config_entry", ")", ":", "self", ".", "_data_key", "=", "\"rgb\"", "self", ".", "_hs", "=", "(", "0", ",", "0", ")", "self", ".", "_brightness", "=", "100", "super", "(", ")", ".", "__init__", "(", "device", ",", "name", ",", "xiaomi_hub", ",", "config_entry", ")" ]
[ 36, 4 ]
[ 42, 64 ]
python
en
['en', 'pl', 'en']
True
XiaomiGatewayLight.is_on
(self)
Return true if it is on.
Return true if it is on.
def is_on(self): """Return true if it is on.""" return self._state
[ "def", "is_on", "(", "self", ")", ":", "return", "self", ".", "_state" ]
[ 45, 4 ]
[ 47, 26 ]
python
en
['en', 'fy', 'en']
True
XiaomiGatewayLight.parse_data
(self, data, raw_data)
Parse data sent by gateway.
Parse data sent by gateway.
def parse_data(self, data, raw_data): """Parse data sent by gateway.""" value = data.get(self._data_key) if value is None: return False if value == 0: if self._state: self._state = False return True rgbhexstr = "%x" % value if len(rgbhexstr) > 8: _LOGGER.error( "Light RGB data error." " Can't be more than 8 characters. Received: %s", rgbhexstr, ) return False rgbhexstr = rgbhexstr.zfill(8) rgbhex = bytes.fromhex(rgbhexstr) rgba = struct.unpack("BBBB", rgbhex) brightness = rgba[0] rgb = rgba[1:] self._brightness = brightness self._hs = color_util.color_RGB_to_hs(*rgb) self._state = True return True
[ "def", "parse_data", "(", "self", ",", "data", ",", "raw_data", ")", ":", "value", "=", "data", ".", "get", "(", "self", ".", "_data_key", ")", "if", "value", "is", "None", ":", "return", "False", "if", "value", "==", "0", ":", "if", "self", ".", "_state", ":", "self", ".", "_state", "=", "False", "return", "True", "rgbhexstr", "=", "\"%x\"", "%", "value", "if", "len", "(", "rgbhexstr", ")", ">", "8", ":", "_LOGGER", ".", "error", "(", "\"Light RGB data error.\"", "\" Can't be more than 8 characters. Received: %s\"", ",", "rgbhexstr", ",", ")", "return", "False", "rgbhexstr", "=", "rgbhexstr", ".", "zfill", "(", "8", ")", "rgbhex", "=", "bytes", ".", "fromhex", "(", "rgbhexstr", ")", "rgba", "=", "struct", ".", "unpack", "(", "\"BBBB\"", ",", "rgbhex", ")", "brightness", "=", "rgba", "[", "0", "]", "rgb", "=", "rgba", "[", "1", ":", "]", "self", ".", "_brightness", "=", "brightness", "self", ".", "_hs", "=", "color_util", ".", "color_RGB_to_hs", "(", "*", "rgb", ")", "self", ".", "_state", "=", "True", "return", "True" ]
[ 49, 4 ]
[ 78, 19 ]
python
en
['en', 'de', 'en']
True
XiaomiGatewayLight.brightness
(self)
Return the brightness of this light between 0..255.
Return the brightness of this light between 0..255.
def brightness(self): """Return the brightness of this light between 0..255.""" return int(255 * self._brightness / 100)
[ "def", "brightness", "(", "self", ")", ":", "return", "int", "(", "255", "*", "self", ".", "_brightness", "/", "100", ")" ]
[ 81, 4 ]
[ 83, 48 ]
python
en
['en', 'en', 'en']
True
XiaomiGatewayLight.hs_color
(self)
Return the hs color value.
Return the hs color value.
def hs_color(self): """Return the hs color value.""" return self._hs
[ "def", "hs_color", "(", "self", ")", ":", "return", "self", ".", "_hs" ]
[ 86, 4 ]
[ 88, 23 ]
python
en
['en', 'en', 'en']
True
XiaomiGatewayLight.supported_features
(self)
Return the supported features.
Return the supported features.
def supported_features(self): """Return the supported features.""" return SUPPORT_BRIGHTNESS | SUPPORT_COLOR
[ "def", "supported_features", "(", "self", ")", ":", "return", "SUPPORT_BRIGHTNESS", "|", "SUPPORT_COLOR" ]
[ 91, 4 ]
[ 93, 49 ]
python
en
['en', 'en', 'en']
True
XiaomiGatewayLight.turn_on
(self, **kwargs)
Turn the light on.
Turn the light on.
def turn_on(self, **kwargs): """Turn the light on.""" if ATTR_HS_COLOR in kwargs: self._hs = kwargs[ATTR_HS_COLOR] if ATTR_BRIGHTNESS in kwargs: self._brightness = int(100 * kwargs[ATTR_BRIGHTNESS] / 255) rgb = color_util.color_hs_to_RGB(*self._hs) rgba = (self._brightness,) + rgb rgbhex = binascii.hexlify(struct.pack("BBBB", *rgba)).decode("ASCII") rgbhex = int(rgbhex, 16) if self._write_to_hub(self._sid, **{self._data_key: rgbhex}): self._state = True self.schedule_update_ha_state()
[ "def", "turn_on", "(", "self", ",", "*", "*", "kwargs", ")", ":", "if", "ATTR_HS_COLOR", "in", "kwargs", ":", "self", ".", "_hs", "=", "kwargs", "[", "ATTR_HS_COLOR", "]", "if", "ATTR_BRIGHTNESS", "in", "kwargs", ":", "self", ".", "_brightness", "=", "int", "(", "100", "*", "kwargs", "[", "ATTR_BRIGHTNESS", "]", "/", "255", ")", "rgb", "=", "color_util", ".", "color_hs_to_RGB", "(", "*", "self", ".", "_hs", ")", "rgba", "=", "(", "self", ".", "_brightness", ",", ")", "+", "rgb", "rgbhex", "=", "binascii", ".", "hexlify", "(", "struct", ".", "pack", "(", "\"BBBB\"", ",", "*", "rgba", ")", ")", ".", "decode", "(", "\"ASCII\"", ")", "rgbhex", "=", "int", "(", "rgbhex", ",", "16", ")", "if", "self", ".", "_write_to_hub", "(", "self", ".", "_sid", ",", "*", "*", "{", "self", ".", "_data_key", ":", "rgbhex", "}", ")", ":", "self", ".", "_state", "=", "True", "self", ".", "schedule_update_ha_state", "(", ")" ]
[ 95, 4 ]
[ 110, 43 ]
python
en
['en', 'et', 'en']
True
XiaomiGatewayLight.turn_off
(self, **kwargs)
Turn the light off.
Turn the light off.
def turn_off(self, **kwargs): """Turn the light off.""" if self._write_to_hub(self._sid, **{self._data_key: 0}): self._state = False self.schedule_update_ha_state()
[ "def", "turn_off", "(", "self", ",", "*", "*", "kwargs", ")", ":", "if", "self", ".", "_write_to_hub", "(", "self", ".", "_sid", ",", "*", "*", "{", "self", ".", "_data_key", ":", "0", "}", ")", ":", "self", ".", "_state", "=", "False", "self", ".", "schedule_update_ha_state", "(", ")" ]
[ 112, 4 ]
[ 116, 43 ]
python
en
['en', 'zh', 'en']
True
async_setup_entry
(hass, config_entry, async_add_entities)
Set up the Rachio binary sensors.
Set up the Rachio binary sensors.
async def async_setup_entry(hass, config_entry, async_add_entities): """Set up the Rachio binary sensors.""" entities = await hass.async_add_executor_job(_create_entities, hass, config_entry) async_add_entities(entities) _LOGGER.info("%d Rachio binary sensor(s) added", len(entities))
[ "async", "def", "async_setup_entry", "(", "hass", ",", "config_entry", ",", "async_add_entities", ")", ":", "entities", "=", "await", "hass", ".", "async_add_executor_job", "(", "_create_entities", ",", "hass", ",", "config_entry", ")", "async_add_entities", "(", "entities", ")", "_LOGGER", ".", "info", "(", "\"%d Rachio binary sensor(s) added\"", ",", "len", "(", "entities", ")", ")" ]
[ 34, 0 ]
[ 38, 67 ]
python
en
['en', 'mi', 'en']
True
RachioControllerBinarySensor.__init__
(self, controller)
Set up a new Rachio controller binary sensor.
Set up a new Rachio controller binary sensor.
def __init__(self, controller): """Set up a new Rachio controller binary sensor.""" super().__init__(controller) self._state = None
[ "def", "__init__", "(", "self", ",", "controller", ")", ":", "super", "(", ")", ".", "__init__", "(", "controller", ")", "self", ".", "_state", "=", "None" ]
[ 52, 4 ]
[ 55, 26 ]
python
en
['en', 'gl', 'en']
True
RachioControllerBinarySensor.is_on
(self)
Return whether the sensor has a 'true' value.
Return whether the sensor has a 'true' value.
def is_on(self) -> bool: """Return whether the sensor has a 'true' value.""" return self._state
[ "def", "is_on", "(", "self", ")", "->", "bool", ":", "return", "self", ".", "_state" ]
[ 58, 4 ]
[ 60, 26 ]
python
en
['en', 'pt', 'en']
True
RachioControllerBinarySensor._async_handle_any_update
(self, *args, **kwargs)
Determine whether an update event applies to this device.
Determine whether an update event applies to this device.
def _async_handle_any_update(self, *args, **kwargs) -> None: """Determine whether an update event applies to this device.""" if args[0][KEY_DEVICE_ID] != self._controller.controller_id: # For another device return # For this device self._async_handle_update(args, kwargs)
[ "def", "_async_handle_any_update", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", "->", "None", ":", "if", "args", "[", "0", "]", "[", "KEY_DEVICE_ID", "]", "!=", "self", ".", "_controller", ".", "controller_id", ":", "# For another device", "return", "# For this device", "self", ".", "_async_handle_update", "(", "args", ",", "kwargs", ")" ]
[ 63, 4 ]
[ 70, 47 ]
python
en
['en', 'en', 'en']
True
RachioControllerBinarySensor._async_handle_update
(self, *args, **kwargs)
Handle an update to the state of this sensor.
Handle an update to the state of this sensor.
def _async_handle_update(self, *args, **kwargs) -> None: """Handle an update to the state of this sensor."""
[ "def", "_async_handle_update", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", "->", "None", ":" ]
[ 73, 4 ]
[ 74, 59 ]
python
en
['en', 'en', 'en']
True
RachioControllerOnlineBinarySensor.name
(self)
Return the name of this sensor including the controller name.
Return the name of this sensor including the controller name.
def name(self) -> str: """Return the name of this sensor including the controller name.""" return self._controller.name
[ "def", "name", "(", "self", ")", "->", "str", ":", "return", "self", ".", "_controller", ".", "name" ]
[ 81, 4 ]
[ 83, 36 ]
python
en
['en', 'en', 'en']
True
RachioControllerOnlineBinarySensor.unique_id
(self)
Return a unique id for this entity.
Return a unique id for this entity.
def unique_id(self) -> str: """Return a unique id for this entity.""" return f"{self._controller.controller_id}-online"
[ "def", "unique_id", "(", "self", ")", "->", "str", ":", "return", "f\"{self._controller.controller_id}-online\"" ]
[ 86, 4 ]
[ 88, 57 ]
python
en
['en', 'en', 'en']
True
RachioControllerOnlineBinarySensor.device_class
(self)
Return the class of this device, from component DEVICE_CLASSES.
Return the class of this device, from component DEVICE_CLASSES.
def device_class(self) -> str: """Return the class of this device, from component DEVICE_CLASSES.""" return DEVICE_CLASS_CONNECTIVITY
[ "def", "device_class", "(", "self", ")", "->", "str", ":", "return", "DEVICE_CLASS_CONNECTIVITY" ]
[ 91, 4 ]
[ 93, 40 ]
python
en
['en', 'en', 'en']
True
RachioControllerOnlineBinarySensor.icon
(self)
Return the name of an icon for this sensor.
Return the name of an icon for this sensor.
def icon(self) -> str: """Return the name of an icon for this sensor.""" return "mdi:wifi-strength-4" if self.is_on else "mdi:wifi-strength-off-outline"
[ "def", "icon", "(", "self", ")", "->", "str", ":", "return", "\"mdi:wifi-strength-4\"", "if", "self", ".", "is_on", "else", "\"mdi:wifi-strength-off-outline\"" ]
[ 96, 4 ]
[ 98, 87 ]
python
en
['en', 'en', 'en']
True
RachioControllerOnlineBinarySensor._async_handle_update
(self, *args, **kwargs)
Handle an update to the state of this sensor.
Handle an update to the state of this sensor.
def _async_handle_update(self, *args, **kwargs) -> None: """Handle an update to the state of this sensor.""" if ( args[0][0][KEY_SUBTYPE] == SUBTYPE_ONLINE or args[0][0][KEY_SUBTYPE] == SUBTYPE_COLD_REBOOT ): self._state = True elif args[0][0][KEY_SUBTYPE] == SUBTYPE_OFFLINE: self._state = False self.async_write_ha_state()
[ "def", "_async_handle_update", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", "->", "None", ":", "if", "(", "args", "[", "0", "]", "[", "0", "]", "[", "KEY_SUBTYPE", "]", "==", "SUBTYPE_ONLINE", "or", "args", "[", "0", "]", "[", "0", "]", "[", "KEY_SUBTYPE", "]", "==", "SUBTYPE_COLD_REBOOT", ")", ":", "self", ".", "_state", "=", "True", "elif", "args", "[", "0", "]", "[", "0", "]", "[", "KEY_SUBTYPE", "]", "==", "SUBTYPE_OFFLINE", ":", "self", ".", "_state", "=", "False", "self", ".", "async_write_ha_state", "(", ")" ]
[ 101, 4 ]
[ 111, 35 ]
python
en
['en', 'en', 'en']
True
RachioControllerOnlineBinarySensor.async_added_to_hass
(self)
Subscribe to updates.
Subscribe to updates.
async def async_added_to_hass(self): """Subscribe to updates.""" self._state = self._controller.init_data[KEY_STATUS] == STATUS_ONLINE self.async_on_remove( async_dispatcher_connect( self.hass, SIGNAL_RACHIO_CONTROLLER_UPDATE, self._async_handle_any_update, ) )
[ "async", "def", "async_added_to_hass", "(", "self", ")", ":", "self", ".", "_state", "=", "self", ".", "_controller", ".", "init_data", "[", "KEY_STATUS", "]", "==", "STATUS_ONLINE", "self", ".", "async_on_remove", "(", "async_dispatcher_connect", "(", "self", ".", "hass", ",", "SIGNAL_RACHIO_CONTROLLER_UPDATE", ",", "self", ".", "_async_handle_any_update", ",", ")", ")" ]
[ 113, 4 ]
[ 123, 9 ]
python
en
['en', 'en', 'en']
True
RachioRainSensor.name
(self)
Return the name of this sensor including the controller name.
Return the name of this sensor including the controller name.
def name(self) -> str: """Return the name of this sensor including the controller name.""" return f"{self._controller.name} rain sensor"
[ "def", "name", "(", "self", ")", "->", "str", ":", "return", "f\"{self._controller.name} rain sensor\"" ]
[ 130, 4 ]
[ 132, 53 ]
python
en
['en', 'en', 'en']
True
RachioRainSensor.unique_id
(self)
Return a unique id for this entity.
Return a unique id for this entity.
def unique_id(self) -> str: """Return a unique id for this entity.""" return f"{self._controller.controller_id}-rain_sensor"
[ "def", "unique_id", "(", "self", ")", "->", "str", ":", "return", "f\"{self._controller.controller_id}-rain_sensor\"" ]
[ 135, 4 ]
[ 137, 62 ]
python
en
['en', 'en', 'en']
True
RachioRainSensor.device_class
(self)
Return the class of this device.
Return the class of this device.
def device_class(self) -> str: """Return the class of this device.""" return DEVICE_CLASS_MOISTURE
[ "def", "device_class", "(", "self", ")", "->", "str", ":", "return", "DEVICE_CLASS_MOISTURE" ]
[ 140, 4 ]
[ 142, 36 ]
python
en
['en', 'en', 'en']
True
RachioRainSensor.icon
(self)
Return the icon for this sensor.
Return the icon for this sensor.
def icon(self) -> str: """Return the icon for this sensor.""" return "mdi:water" if self.is_on else "mdi:water-off"
[ "def", "icon", "(", "self", ")", "->", "str", ":", "return", "\"mdi:water\"", "if", "self", ".", "is_on", "else", "\"mdi:water-off\"" ]
[ 145, 4 ]
[ 147, 61 ]
python
en
['en', 'en', 'en']
True
RachioRainSensor._async_handle_update
(self, *args, **kwargs)
Handle an update to the state of this sensor.
Handle an update to the state of this sensor.
def _async_handle_update(self, *args, **kwargs) -> None: """Handle an update to the state of this sensor.""" if args[0][0][KEY_SUBTYPE] == SUBTYPE_RAIN_SENSOR_DETECTION_ON: self._state = True elif args[0][0][KEY_SUBTYPE] == SUBTYPE_RAIN_SENSOR_DETECTION_OFF: self._state = False self.async_write_ha_state()
[ "def", "_async_handle_update", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", "->", "None", ":", "if", "args", "[", "0", "]", "[", "0", "]", "[", "KEY_SUBTYPE", "]", "==", "SUBTYPE_RAIN_SENSOR_DETECTION_ON", ":", "self", ".", "_state", "=", "True", "elif", "args", "[", "0", "]", "[", "0", "]", "[", "KEY_SUBTYPE", "]", "==", "SUBTYPE_RAIN_SENSOR_DETECTION_OFF", ":", "self", ".", "_state", "=", "False", "self", ".", "async_write_ha_state", "(", ")" ]
[ 150, 4 ]
[ 157, 35 ]
python
en
['en', 'en', 'en']
True
RachioRainSensor.async_added_to_hass
(self)
Subscribe to updates.
Subscribe to updates.
async def async_added_to_hass(self): """Subscribe to updates.""" self._state = self._controller.init_data[KEY_RAIN_SENSOR_TRIPPED] self.async_on_remove( async_dispatcher_connect( self.hass, SIGNAL_RACHIO_RAIN_SENSOR_UPDATE, self._async_handle_any_update, ) )
[ "async", "def", "async_added_to_hass", "(", "self", ")", ":", "self", ".", "_state", "=", "self", ".", "_controller", ".", "init_data", "[", "KEY_RAIN_SENSOR_TRIPPED", "]", "self", ".", "async_on_remove", "(", "async_dispatcher_connect", "(", "self", ".", "hass", ",", "SIGNAL_RACHIO_RAIN_SENSOR_UPDATE", ",", "self", ".", "_async_handle_any_update", ",", ")", ")" ]
[ 159, 4 ]
[ 169, 9 ]
python
en
['en', 'en', 'en']
True
test_pr3000rt2u
(hass)
Test creation of PR3000RT2U sensors.
Test creation of PR3000RT2U sensors.
async def test_pr3000rt2u(hass): """Test creation of PR3000RT2U sensors.""" await async_init_integration(hass, "PR3000RT2U", ["battery.charge"]) registry = await hass.helpers.entity_registry.async_get_registry() entry = registry.async_get("sensor.ups1_battery_charge") assert entry assert entry.unique_id == "CPS_PR3000RT2U_PYVJO2000034_battery.charge" state = hass.states.get("sensor.ups1_battery_charge") assert state.state == "100" expected_attributes = { "device_class": "battery", "friendly_name": "Ups1 Battery Charge", "state": "Online", "unit_of_measurement": PERCENTAGE, } # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( state.attributes[key] == expected_attributes[key] for key in expected_attributes )
[ "async", "def", "test_pr3000rt2u", "(", "hass", ")", ":", "await", "async_init_integration", "(", "hass", ",", "\"PR3000RT2U\"", ",", "[", "\"battery.charge\"", "]", ")", "registry", "=", "await", "hass", ".", "helpers", ".", "entity_registry", ".", "async_get_registry", "(", ")", "entry", "=", "registry", ".", "async_get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "entry", "assert", "entry", ".", "unique_id", "==", "\"CPS_PR3000RT2U_PYVJO2000034_battery.charge\"", "state", "=", "hass", ".", "states", ".", "get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "state", ".", "state", "==", "\"100\"", "expected_attributes", "=", "{", "\"device_class\"", ":", "\"battery\"", ",", "\"friendly_name\"", ":", "\"Ups1 Battery Charge\"", ",", "\"state\"", ":", "\"Online\"", ",", "\"unit_of_measurement\"", ":", "PERCENTAGE", ",", "}", "# Only test for a subset of attributes in case", "# HA changes the implementation and a new one appears", "assert", "all", "(", "state", ".", "attributes", "[", "key", "]", "==", "expected_attributes", "[", "key", "]", "for", "key", "in", "expected_attributes", ")" ]
[ 7, 0 ]
[ 29, 5 ]
python
en
['en', 'et', 'en']
True
test_cp1350c
(hass)
Test creation of CP1350C sensors.
Test creation of CP1350C sensors.
async def test_cp1350c(hass): """Test creation of CP1350C sensors.""" config_entry = await async_init_integration(hass, "CP1350C", ["battery.charge"]) registry = await hass.helpers.entity_registry.async_get_registry() entry = registry.async_get("sensor.ups1_battery_charge") assert entry assert entry.unique_id == f"{config_entry.entry_id}_battery.charge" state = hass.states.get("sensor.ups1_battery_charge") assert state.state == "100" expected_attributes = { "device_class": "battery", "friendly_name": "Ups1 Battery Charge", "state": "Online", "unit_of_measurement": PERCENTAGE, } # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( state.attributes[key] == expected_attributes[key] for key in expected_attributes )
[ "async", "def", "test_cp1350c", "(", "hass", ")", ":", "config_entry", "=", "await", "async_init_integration", "(", "hass", ",", "\"CP1350C\"", ",", "[", "\"battery.charge\"", "]", ")", "registry", "=", "await", "hass", ".", "helpers", ".", "entity_registry", ".", "async_get_registry", "(", ")", "entry", "=", "registry", ".", "async_get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "entry", "assert", "entry", ".", "unique_id", "==", "f\"{config_entry.entry_id}_battery.charge\"", "state", "=", "hass", ".", "states", ".", "get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "state", ".", "state", "==", "\"100\"", "expected_attributes", "=", "{", "\"device_class\"", ":", "\"battery\"", ",", "\"friendly_name\"", ":", "\"Ups1 Battery Charge\"", ",", "\"state\"", ":", "\"Online\"", ",", "\"unit_of_measurement\"", ":", "PERCENTAGE", ",", "}", "# Only test for a subset of attributes in case", "# HA changes the implementation and a new one appears", "assert", "all", "(", "state", ".", "attributes", "[", "key", "]", "==", "expected_attributes", "[", "key", "]", "for", "key", "in", "expected_attributes", ")" ]
[ 32, 0 ]
[ 55, 5 ]
python
en
['en', 'en', 'en']
True
test_5e850i
(hass)
Test creation of 5E850I sensors.
Test creation of 5E850I sensors.
async def test_5e850i(hass): """Test creation of 5E850I sensors.""" config_entry = await async_init_integration(hass, "5E850I", ["battery.charge"]) registry = await hass.helpers.entity_registry.async_get_registry() entry = registry.async_get("sensor.ups1_battery_charge") assert entry assert entry.unique_id == f"{config_entry.entry_id}_battery.charge" state = hass.states.get("sensor.ups1_battery_charge") assert state.state == "100" expected_attributes = { "device_class": "battery", "friendly_name": "Ups1 Battery Charge", "state": "Online", "unit_of_measurement": PERCENTAGE, } # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( state.attributes[key] == expected_attributes[key] for key in expected_attributes )
[ "async", "def", "test_5e850i", "(", "hass", ")", ":", "config_entry", "=", "await", "async_init_integration", "(", "hass", ",", "\"5E850I\"", ",", "[", "\"battery.charge\"", "]", ")", "registry", "=", "await", "hass", ".", "helpers", ".", "entity_registry", ".", "async_get_registry", "(", ")", "entry", "=", "registry", ".", "async_get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "entry", "assert", "entry", ".", "unique_id", "==", "f\"{config_entry.entry_id}_battery.charge\"", "state", "=", "hass", ".", "states", ".", "get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "state", ".", "state", "==", "\"100\"", "expected_attributes", "=", "{", "\"device_class\"", ":", "\"battery\"", ",", "\"friendly_name\"", ":", "\"Ups1 Battery Charge\"", ",", "\"state\"", ":", "\"Online\"", ",", "\"unit_of_measurement\"", ":", "PERCENTAGE", ",", "}", "# Only test for a subset of attributes in case", "# HA changes the implementation and a new one appears", "assert", "all", "(", "state", ".", "attributes", "[", "key", "]", "==", "expected_attributes", "[", "key", "]", "for", "key", "in", "expected_attributes", ")" ]
[ 58, 0 ]
[ 80, 5 ]
python
en
['en', 'sm', 'en']
True
test_5e650i
(hass)
Test creation of 5E650I sensors.
Test creation of 5E650I sensors.
async def test_5e650i(hass): """Test creation of 5E650I sensors.""" config_entry = await async_init_integration(hass, "5E650I", ["battery.charge"]) registry = await hass.helpers.entity_registry.async_get_registry() entry = registry.async_get("sensor.ups1_battery_charge") assert entry assert entry.unique_id == f"{config_entry.entry_id}_battery.charge" state = hass.states.get("sensor.ups1_battery_charge") assert state.state == "100" expected_attributes = { "device_class": "battery", "friendly_name": "Ups1 Battery Charge", "state": "Online Battery Charging", "unit_of_measurement": PERCENTAGE, } # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( state.attributes[key] == expected_attributes[key] for key in expected_attributes )
[ "async", "def", "test_5e650i", "(", "hass", ")", ":", "config_entry", "=", "await", "async_init_integration", "(", "hass", ",", "\"5E650I\"", ",", "[", "\"battery.charge\"", "]", ")", "registry", "=", "await", "hass", ".", "helpers", ".", "entity_registry", ".", "async_get_registry", "(", ")", "entry", "=", "registry", ".", "async_get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "entry", "assert", "entry", ".", "unique_id", "==", "f\"{config_entry.entry_id}_battery.charge\"", "state", "=", "hass", ".", "states", ".", "get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "state", ".", "state", "==", "\"100\"", "expected_attributes", "=", "{", "\"device_class\"", ":", "\"battery\"", ",", "\"friendly_name\"", ":", "\"Ups1 Battery Charge\"", ",", "\"state\"", ":", "\"Online Battery Charging\"", ",", "\"unit_of_measurement\"", ":", "PERCENTAGE", ",", "}", "# Only test for a subset of attributes in case", "# HA changes the implementation and a new one appears", "assert", "all", "(", "state", ".", "attributes", "[", "key", "]", "==", "expected_attributes", "[", "key", "]", "for", "key", "in", "expected_attributes", ")" ]
[ 83, 0 ]
[ 105, 5 ]
python
en
['en', 'sm', 'en']
True
test_backupsses600m1
(hass)
Test creation of BACKUPSES600M1 sensors.
Test creation of BACKUPSES600M1 sensors.
async def test_backupsses600m1(hass): """Test creation of BACKUPSES600M1 sensors.""" await async_init_integration(hass, "BACKUPSES600M1", ["battery.charge"]) registry = await hass.helpers.entity_registry.async_get_registry() entry = registry.async_get("sensor.ups1_battery_charge") assert entry assert ( entry.unique_id == "American Power Conversion_Back-UPS ES 600M1_4B1713P32195 _battery.charge" ) state = hass.states.get("sensor.ups1_battery_charge") assert state.state == "100" expected_attributes = { "device_class": "battery", "friendly_name": "Ups1 Battery Charge", "state": "Online", "unit_of_measurement": PERCENTAGE, } # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( state.attributes[key] == expected_attributes[key] for key in expected_attributes )
[ "async", "def", "test_backupsses600m1", "(", "hass", ")", ":", "await", "async_init_integration", "(", "hass", ",", "\"BACKUPSES600M1\"", ",", "[", "\"battery.charge\"", "]", ")", "registry", "=", "await", "hass", ".", "helpers", ".", "entity_registry", ".", "async_get_registry", "(", ")", "entry", "=", "registry", ".", "async_get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "entry", "assert", "(", "entry", ".", "unique_id", "==", "\"American Power Conversion_Back-UPS ES 600M1_4B1713P32195 _battery.charge\"", ")", "state", "=", "hass", ".", "states", ".", "get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "state", ".", "state", "==", "\"100\"", "expected_attributes", "=", "{", "\"device_class\"", ":", "\"battery\"", ",", "\"friendly_name\"", ":", "\"Ups1 Battery Charge\"", ",", "\"state\"", ":", "\"Online\"", ",", "\"unit_of_measurement\"", ":", "PERCENTAGE", ",", "}", "# Only test for a subset of attributes in case", "# HA changes the implementation and a new one appears", "assert", "all", "(", "state", ".", "attributes", "[", "key", "]", "==", "expected_attributes", "[", "key", "]", "for", "key", "in", "expected_attributes", ")" ]
[ 108, 0 ]
[ 133, 5 ]
python
en
['en', 'da', 'en']
True
test_cp1500pfclcd
(hass)
Test creation of CP1500PFCLCD sensors.
Test creation of CP1500PFCLCD sensors.
async def test_cp1500pfclcd(hass): """Test creation of CP1500PFCLCD sensors.""" config_entry = await async_init_integration( hass, "CP1500PFCLCD", ["battery.charge"] ) registry = await hass.helpers.entity_registry.async_get_registry() entry = registry.async_get("sensor.ups1_battery_charge") assert entry assert entry.unique_id == f"{config_entry.entry_id}_battery.charge" state = hass.states.get("sensor.ups1_battery_charge") assert state.state == "100" expected_attributes = { "device_class": "battery", "friendly_name": "Ups1 Battery Charge", "state": "Online", "unit_of_measurement": PERCENTAGE, } # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( state.attributes[key] == expected_attributes[key] for key in expected_attributes )
[ "async", "def", "test_cp1500pfclcd", "(", "hass", ")", ":", "config_entry", "=", "await", "async_init_integration", "(", "hass", ",", "\"CP1500PFCLCD\"", ",", "[", "\"battery.charge\"", "]", ")", "registry", "=", "await", "hass", ".", "helpers", ".", "entity_registry", ".", "async_get_registry", "(", ")", "entry", "=", "registry", ".", "async_get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "entry", "assert", "entry", ".", "unique_id", "==", "f\"{config_entry.entry_id}_battery.charge\"", "state", "=", "hass", ".", "states", ".", "get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "state", ".", "state", "==", "\"100\"", "expected_attributes", "=", "{", "\"device_class\"", ":", "\"battery\"", ",", "\"friendly_name\"", ":", "\"Ups1 Battery Charge\"", ",", "\"state\"", ":", "\"Online\"", ",", "\"unit_of_measurement\"", ":", "PERCENTAGE", ",", "}", "# Only test for a subset of attributes in case", "# HA changes the implementation and a new one appears", "assert", "all", "(", "state", ".", "attributes", "[", "key", "]", "==", "expected_attributes", "[", "key", "]", "for", "key", "in", "expected_attributes", ")" ]
[ 136, 0 ]
[ 160, 5 ]
python
en
['en', 'en', 'en']
True
test_dl650elcd
(hass)
Test creation of DL650ELCD sensors.
Test creation of DL650ELCD sensors.
async def test_dl650elcd(hass): """Test creation of DL650ELCD sensors.""" config_entry = await async_init_integration(hass, "DL650ELCD", ["battery.charge"]) registry = await hass.helpers.entity_registry.async_get_registry() entry = registry.async_get("sensor.ups1_battery_charge") assert entry assert entry.unique_id == f"{config_entry.entry_id}_battery.charge" state = hass.states.get("sensor.ups1_battery_charge") assert state.state == "100" expected_attributes = { "device_class": "battery", "friendly_name": "Ups1 Battery Charge", "state": "Online", "unit_of_measurement": PERCENTAGE, } # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( state.attributes[key] == expected_attributes[key] for key in expected_attributes )
[ "async", "def", "test_dl650elcd", "(", "hass", ")", ":", "config_entry", "=", "await", "async_init_integration", "(", "hass", ",", "\"DL650ELCD\"", ",", "[", "\"battery.charge\"", "]", ")", "registry", "=", "await", "hass", ".", "helpers", ".", "entity_registry", ".", "async_get_registry", "(", ")", "entry", "=", "registry", ".", "async_get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "entry", "assert", "entry", ".", "unique_id", "==", "f\"{config_entry.entry_id}_battery.charge\"", "state", "=", "hass", ".", "states", ".", "get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "state", ".", "state", "==", "\"100\"", "expected_attributes", "=", "{", "\"device_class\"", ":", "\"battery\"", ",", "\"friendly_name\"", ":", "\"Ups1 Battery Charge\"", ",", "\"state\"", ":", "\"Online\"", ",", "\"unit_of_measurement\"", ":", "PERCENTAGE", ",", "}", "# Only test for a subset of attributes in case", "# HA changes the implementation and a new one appears", "assert", "all", "(", "state", ".", "attributes", "[", "key", "]", "==", "expected_attributes", "[", "key", "]", "for", "key", "in", "expected_attributes", ")" ]
[ 163, 0 ]
[ 185, 5 ]
python
en
['en', 'da', 'en']
True
test_blazer_usb
(hass)
Test creation of blazer_usb sensors.
Test creation of blazer_usb sensors.
async def test_blazer_usb(hass): """Test creation of blazer_usb sensors.""" config_entry = await async_init_integration(hass, "blazer_usb", ["battery.charge"]) registry = await hass.helpers.entity_registry.async_get_registry() entry = registry.async_get("sensor.ups1_battery_charge") assert entry assert entry.unique_id == f"{config_entry.entry_id}_battery.charge" state = hass.states.get("sensor.ups1_battery_charge") assert state.state == "100" expected_attributes = { "device_class": "battery", "friendly_name": "Ups1 Battery Charge", "state": "Online", "unit_of_measurement": PERCENTAGE, } # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( state.attributes[key] == expected_attributes[key] for key in expected_attributes )
[ "async", "def", "test_blazer_usb", "(", "hass", ")", ":", "config_entry", "=", "await", "async_init_integration", "(", "hass", ",", "\"blazer_usb\"", ",", "[", "\"battery.charge\"", "]", ")", "registry", "=", "await", "hass", ".", "helpers", ".", "entity_registry", ".", "async_get_registry", "(", ")", "entry", "=", "registry", ".", "async_get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "entry", "assert", "entry", ".", "unique_id", "==", "f\"{config_entry.entry_id}_battery.charge\"", "state", "=", "hass", ".", "states", ".", "get", "(", "\"sensor.ups1_battery_charge\"", ")", "assert", "state", ".", "state", "==", "\"100\"", "expected_attributes", "=", "{", "\"device_class\"", ":", "\"battery\"", ",", "\"friendly_name\"", ":", "\"Ups1 Battery Charge\"", ",", "\"state\"", ":", "\"Online\"", ",", "\"unit_of_measurement\"", ":", "PERCENTAGE", ",", "}", "# Only test for a subset of attributes in case", "# HA changes the implementation and a new one appears", "assert", "all", "(", "state", ".", "attributes", "[", "key", "]", "==", "expected_attributes", "[", "key", "]", "for", "key", "in", "expected_attributes", ")" ]
[ 188, 0 ]
[ 210, 5 ]
python
en
['en', 'ko', 'en']
True
async_setup
(hass, config)
Set up the Roon platform.
Set up the Roon platform.
async def async_setup(hass, config): """Set up the Roon platform.""" hass.data[DOMAIN] = {} return True
[ "async", "def", "async_setup", "(", "hass", ",", "config", ")", ":", "hass", ".", "data", "[", "DOMAIN", "]", "=", "{", "}", "return", "True" ]
[ 8, 0 ]
[ 11, 15 ]
python
en
['en', 'da', 'en']
True
async_setup_entry
(hass, entry)
Set up a roonserver from a config entry.
Set up a roonserver from a config entry.
async def async_setup_entry(hass, entry): """Set up a roonserver from a config entry.""" host = entry.data[CONF_HOST] roonserver = RoonServer(hass, entry) if not await roonserver.async_setup(): return False hass.data[DOMAIN][entry.entry_id] = roonserver device_registry = await dr.async_get_registry(hass) device_registry.async_get_or_create( config_entry_id=entry.entry_id, identifiers={(DOMAIN, entry.entry_id)}, manufacturer="Roonlabs", name=host, ) return True
[ "async", "def", "async_setup_entry", "(", "hass", ",", "entry", ")", ":", "host", "=", "entry", ".", "data", "[", "CONF_HOST", "]", "roonserver", "=", "RoonServer", "(", "hass", ",", "entry", ")", "if", "not", "await", "roonserver", ".", "async_setup", "(", ")", ":", "return", "False", "hass", ".", "data", "[", "DOMAIN", "]", "[", "entry", ".", "entry_id", "]", "=", "roonserver", "device_registry", "=", "await", "dr", ".", "async_get_registry", "(", "hass", ")", "device_registry", ".", "async_get_or_create", "(", "config_entry_id", "=", "entry", ".", "entry_id", ",", "identifiers", "=", "{", "(", "DOMAIN", ",", "entry", ".", "entry_id", ")", "}", ",", "manufacturer", "=", "\"Roonlabs\"", ",", "name", "=", "host", ",", ")", "return", "True" ]
[ 14, 0 ]
[ 30, 15 ]
python
en
['en', 'en', 'en']
True
async_unload_entry
(hass, entry)
Unload a config entry.
Unload a config entry.
async def async_unload_entry(hass, entry): """Unload a config entry.""" roonserver = hass.data[DOMAIN].pop(entry.entry_id) return await roonserver.async_reset()
[ "async", "def", "async_unload_entry", "(", "hass", ",", "entry", ")", ":", "roonserver", "=", "hass", ".", "data", "[", "DOMAIN", "]", ".", "pop", "(", "entry", ".", "entry_id", ")", "return", "await", "roonserver", ".", "async_reset", "(", ")" ]
[ 33, 0 ]
[ 36, 41 ]
python
en
['en', 'es', 'en']
True
test_get_url_internal
(hass: HomeAssistant)
Test getting an instance URL when the user has set an internal URL.
Test getting an instance URL when the user has set an internal URL.
async def test_get_url_internal(hass: HomeAssistant): """Test getting an instance URL when the user has set an internal URL.""" assert hass.config.internal_url is None with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_current_request=True) # Test with internal URL: http://example.local:8123 await async_process_ha_core_config( hass, {"internal_url": "http://example.local:8123"}, ) assert hass.config.internal_url == "http://example.local:8123" assert _get_internal_url(hass) == "http://example.local:8123" assert _get_internal_url(hass, allow_ip=False) == "http://example.local:8123" with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_standard_port=True) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_ssl=True) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_current_request=True) with patch( "homeassistant.helpers.network._get_request_host", return_value="example.local" ): assert ( _get_internal_url(hass, require_current_request=True) == "http://example.local:8123" ) with pytest.raises(NoURLAvailableError): _get_internal_url( hass, require_current_request=True, require_standard_port=True ) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_current_request=True, require_ssl=True) with patch( "homeassistant.helpers.network._get_request_host", return_value="no_match.example.local", ), pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_current_request=True) # Test with internal URL: https://example.local:8123 await async_process_ha_core_config( hass, {"internal_url": "https://example.local:8123"}, ) assert hass.config.internal_url == "https://example.local:8123" assert _get_internal_url(hass) == "https://example.local:8123" assert _get_internal_url(hass, allow_ip=False) == "https://example.local:8123" assert _get_internal_url(hass, require_ssl=True) == "https://example.local:8123" with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_standard_port=True) # Test with internal URL: http://example.local:80/ await async_process_ha_core_config( hass, {"internal_url": "http://example.local:80/"}, ) assert hass.config.internal_url == "http://example.local:80/" assert _get_internal_url(hass) == "http://example.local" assert _get_internal_url(hass, allow_ip=False) == "http://example.local" assert _get_internal_url(hass, require_standard_port=True) == "http://example.local" with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_ssl=True) # Test with internal URL: https://example.local:443 await async_process_ha_core_config( hass, {"internal_url": "https://example.local:443"}, ) assert hass.config.internal_url == "https://example.local:443" assert _get_internal_url(hass) == "https://example.local" assert _get_internal_url(hass, allow_ip=False) == "https://example.local" assert ( _get_internal_url(hass, require_standard_port=True) == "https://example.local" ) assert _get_internal_url(hass, require_ssl=True) == "https://example.local" # Test with internal URL: https://192.168.0.1 await async_process_ha_core_config( hass, {"internal_url": "https://192.168.0.1"}, ) assert hass.config.internal_url == "https://192.168.0.1" assert _get_internal_url(hass) == "https://192.168.0.1" assert _get_internal_url(hass, require_standard_port=True) == "https://192.168.0.1" assert _get_internal_url(hass, require_ssl=True) == "https://192.168.0.1" with pytest.raises(NoURLAvailableError): _get_internal_url(hass, allow_ip=False) # Test with internal URL: http://192.168.0.1:8123 await async_process_ha_core_config( hass, {"internal_url": "http://192.168.0.1:8123"}, ) assert hass.config.internal_url == "http://192.168.0.1:8123" assert _get_internal_url(hass) == "http://192.168.0.1:8123" with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_standard_port=True) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_ssl=True) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, allow_ip=False) with patch( "homeassistant.helpers.network._get_request_host", return_value="192.168.0.1" ): assert ( _get_internal_url(hass, require_current_request=True) == "http://192.168.0.1:8123" ) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_current_request=True, allow_ip=False) with pytest.raises(NoURLAvailableError): _get_internal_url( hass, require_current_request=True, require_standard_port=True ) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_current_request=True, require_ssl=True)
[ "async", "def", "test_get_url_internal", "(", "hass", ":", "HomeAssistant", ")", ":", "assert", "hass", ".", "config", ".", "internal_url", "is", "None", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "# Test with internal URL: http://example.local:8123", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"internal_url\"", ":", "\"http://example.local:8123\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "internal_url", "==", "\"http://example.local:8123\"", "assert", "_get_internal_url", "(", "hass", ")", "==", "\"http://example.local:8123\"", "assert", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"http://example.local:8123\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"example.local\"", ")", ":", "assert", "(", "_get_internal_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "==", "\"http://example.local:8123\"", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "require_standard_port", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "require_ssl", "=", "True", ")", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"no_match.example.local\"", ",", ")", ",", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "# Test with internal URL: https://example.local:8123", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"internal_url\"", ":", "\"https://example.local:8123\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "internal_url", "==", "\"https://example.local:8123\"", "assert", "_get_internal_url", "(", "hass", ")", "==", "\"https://example.local:8123\"", "assert", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"https://example.local:8123\"", "assert", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://example.local:8123\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "# Test with internal URL: http://example.local:80/", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"internal_url\"", ":", "\"http://example.local:80/\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "internal_url", "==", "\"http://example.local:80/\"", "assert", "_get_internal_url", "(", "hass", ")", "==", "\"http://example.local\"", "assert", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"http://example.local\"", "assert", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"http://example.local\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "# Test with internal URL: https://example.local:443", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"internal_url\"", ":", "\"https://example.local:443\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "internal_url", "==", "\"https://example.local:443\"", "assert", "_get_internal_url", "(", "hass", ")", "==", "\"https://example.local\"", "assert", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"https://example.local\"", "assert", "(", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://example.local\"", ")", "assert", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://example.local\"", "# Test with internal URL: https://192.168.0.1", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"internal_url\"", ":", "\"https://192.168.0.1\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "internal_url", "==", "\"https://192.168.0.1\"", "assert", "_get_internal_url", "(", "hass", ")", "==", "\"https://192.168.0.1\"", "assert", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://192.168.0.1\"", "assert", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://192.168.0.1\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "# Test with internal URL: http://192.168.0.1:8123", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"internal_url\"", ":", "\"http://192.168.0.1:8123\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "internal_url", "==", "\"http://192.168.0.1:8123\"", "assert", "_get_internal_url", "(", "hass", ")", "==", "\"http://192.168.0.1:8123\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"192.168.0.1\"", ")", ":", "assert", "(", "_get_internal_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "==", "\"http://192.168.0.1:8123\"", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "allow_ip", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "require_standard_port", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "require_ssl", "=", "True", ")" ]
[ 20, 0 ]
[ 159, 83 ]
python
en
['en', 'lb', 'en']
True
test_get_url_internal_fallback
(hass: HomeAssistant)
Test getting an instance URL when the user has not set an internal URL.
Test getting an instance URL when the user has not set an internal URL.
async def test_get_url_internal_fallback(hass: HomeAssistant): """Test getting an instance URL when the user has not set an internal URL.""" assert hass.config.internal_url is None hass.config.api = Mock( use_ssl=False, port=8123, deprecated_base_url=None, local_ip="192.168.123.123" ) assert _get_internal_url(hass) == "http://192.168.123.123:8123" with pytest.raises(NoURLAvailableError): _get_internal_url(hass, allow_ip=False) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_standard_port=True) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_ssl=True) hass.config.api = Mock( use_ssl=False, port=80, deprecated_base_url=None, local_ip="192.168.123.123" ) assert _get_internal_url(hass) == "http://192.168.123.123" assert ( _get_internal_url(hass, require_standard_port=True) == "http://192.168.123.123" ) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, allow_ip=False) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_ssl=True) hass.config.api = Mock(use_ssl=True, port=443, deprecated_base_url=None) with pytest.raises(NoURLAvailableError): _get_internal_url(hass) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_standard_port=True) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, allow_ip=False) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_ssl=True) # Do no accept any local loopback address as fallback hass.config.api = Mock( use_ssl=False, port=80, deprecated_base_url=None, local_ip="127.0.0.1" ) with pytest.raises(NoURLAvailableError): _get_internal_url(hass) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_standard_port=True) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, allow_ip=False) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_ssl=True)
[ "async", "def", "test_get_url_internal_fallback", "(", "hass", ":", "HomeAssistant", ")", ":", "assert", "hass", ".", "config", ".", "internal_url", "is", "None", "hass", ".", "config", ".", "api", "=", "Mock", "(", "use_ssl", "=", "False", ",", "port", "=", "8123", ",", "deprecated_base_url", "=", "None", ",", "local_ip", "=", "\"192.168.123.123\"", ")", "assert", "_get_internal_url", "(", "hass", ")", "==", "\"http://192.168.123.123:8123\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "hass", ".", "config", ".", "api", "=", "Mock", "(", "use_ssl", "=", "False", ",", "port", "=", "80", ",", "deprecated_base_url", "=", "None", ",", "local_ip", "=", "\"192.168.123.123\"", ")", "assert", "_get_internal_url", "(", "hass", ")", "==", "\"http://192.168.123.123\"", "assert", "(", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"http://192.168.123.123\"", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "hass", ".", "config", ".", "api", "=", "Mock", "(", "use_ssl", "=", "True", ",", "port", "=", "443", ",", "deprecated_base_url", "=", "None", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "# Do no accept any local loopback address as fallback", "hass", ".", "config", ".", "api", "=", "Mock", "(", "use_ssl", "=", "False", ",", "port", "=", "80", ",", "deprecated_base_url", "=", "None", ",", "local_ip", "=", "\"127.0.0.1\"", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")" ]
[ 162, 0 ]
[ 221, 49 ]
python
en
['en', 'lb', 'en']
True
test_get_url_external
(hass: HomeAssistant)
Test getting an instance URL when the user has set an external URL.
Test getting an instance URL when the user has set an external URL.
async def test_get_url_external(hass: HomeAssistant): """Test getting an instance URL when the user has set an external URL.""" assert hass.config.external_url is None with pytest.raises(NoURLAvailableError): _get_external_url(hass, require_current_request=True) # Test with external URL: http://example.com:8123 await async_process_ha_core_config( hass, {"external_url": "http://example.com:8123"}, ) assert hass.config.external_url == "http://example.com:8123" assert _get_external_url(hass) == "http://example.com:8123" assert _get_external_url(hass, allow_cloud=False) == "http://example.com:8123" assert _get_external_url(hass, allow_ip=False) == "http://example.com:8123" assert _get_external_url(hass, prefer_cloud=True) == "http://example.com:8123" with pytest.raises(NoURLAvailableError): _get_external_url(hass, require_standard_port=True) with pytest.raises(NoURLAvailableError): _get_external_url(hass, require_ssl=True) with pytest.raises(NoURLAvailableError): _get_external_url(hass, require_current_request=True) with patch( "homeassistant.helpers.network._get_request_host", return_value="example.com" ): assert ( _get_external_url(hass, require_current_request=True) == "http://example.com:8123" ) with pytest.raises(NoURLAvailableError): _get_external_url( hass, require_current_request=True, require_standard_port=True ) with pytest.raises(NoURLAvailableError): _get_external_url(hass, require_current_request=True, require_ssl=True) with patch( "homeassistant.helpers.network._get_request_host", return_value="no_match.example.com", ), pytest.raises(NoURLAvailableError): _get_external_url(hass, require_current_request=True) # Test with external URL: http://example.com:80/ await async_process_ha_core_config( hass, {"external_url": "http://example.com:80/"}, ) assert hass.config.external_url == "http://example.com:80/" assert _get_external_url(hass) == "http://example.com" assert _get_external_url(hass, allow_cloud=False) == "http://example.com" assert _get_external_url(hass, allow_ip=False) == "http://example.com" assert _get_external_url(hass, prefer_cloud=True) == "http://example.com" assert _get_external_url(hass, require_standard_port=True) == "http://example.com" with pytest.raises(NoURLAvailableError): _get_external_url(hass, require_ssl=True) # Test with external url: https://example.com:443/ await async_process_ha_core_config( hass, {"external_url": "https://example.com:443/"}, ) assert hass.config.external_url == "https://example.com:443/" assert _get_external_url(hass) == "https://example.com" assert _get_external_url(hass, allow_cloud=False) == "https://example.com" assert _get_external_url(hass, allow_ip=False) == "https://example.com" assert _get_external_url(hass, prefer_cloud=True) == "https://example.com" assert _get_external_url(hass, require_ssl=False) == "https://example.com" assert _get_external_url(hass, require_standard_port=True) == "https://example.com" # Test with external URL: https://example.com:80 await async_process_ha_core_config( hass, {"external_url": "https://example.com:80"}, ) assert hass.config.external_url == "https://example.com:80" assert _get_external_url(hass) == "https://example.com:80" assert _get_external_url(hass, allow_cloud=False) == "https://example.com:80" assert _get_external_url(hass, allow_ip=False) == "https://example.com:80" assert _get_external_url(hass, prefer_cloud=True) == "https://example.com:80" assert _get_external_url(hass, require_ssl=True) == "https://example.com:80" with pytest.raises(NoURLAvailableError): _get_external_url(hass, require_standard_port=True) # Test with external URL: https://192.168.0.1 await async_process_ha_core_config( hass, {"external_url": "https://192.168.0.1"}, ) assert hass.config.external_url == "https://192.168.0.1" assert _get_external_url(hass) == "https://192.168.0.1" assert _get_external_url(hass, allow_cloud=False) == "https://192.168.0.1" assert _get_external_url(hass, prefer_cloud=True) == "https://192.168.0.1" assert _get_external_url(hass, require_standard_port=True) == "https://192.168.0.1" with pytest.raises(NoURLAvailableError): _get_external_url(hass, allow_ip=False) with pytest.raises(NoURLAvailableError): _get_external_url(hass, require_ssl=True) with patch( "homeassistant.helpers.network._get_request_host", return_value="192.168.0.1" ): assert ( _get_external_url(hass, require_current_request=True) == "https://192.168.0.1" ) with pytest.raises(NoURLAvailableError): _get_external_url(hass, require_current_request=True, allow_ip=False) with pytest.raises(NoURLAvailableError): _get_external_url(hass, require_current_request=True, require_ssl=True)
[ "async", "def", "test_get_url_external", "(", "hass", ":", "HomeAssistant", ")", ":", "assert", "hass", ".", "config", ".", "external_url", "is", "None", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_external_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "# Test with external URL: http://example.com:8123", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"external_url\"", ":", "\"http://example.com:8123\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "external_url", "==", "\"http://example.com:8123\"", "assert", "_get_external_url", "(", "hass", ")", "==", "\"http://example.com:8123\"", "assert", "_get_external_url", "(", "hass", ",", "allow_cloud", "=", "False", ")", "==", "\"http://example.com:8123\"", "assert", "_get_external_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"http://example.com:8123\"", "assert", "_get_external_url", "(", "hass", ",", "prefer_cloud", "=", "True", ")", "==", "\"http://example.com:8123\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_external_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_external_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_external_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"example.com\"", ")", ":", "assert", "(", "_get_external_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "==", "\"http://example.com:8123\"", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_external_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "require_standard_port", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_external_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "require_ssl", "=", "True", ")", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"no_match.example.com\"", ",", ")", ",", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_external_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "# Test with external URL: http://example.com:80/", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"external_url\"", ":", "\"http://example.com:80/\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "external_url", "==", "\"http://example.com:80/\"", "assert", "_get_external_url", "(", "hass", ")", "==", "\"http://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "allow_cloud", "=", "False", ")", "==", "\"http://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"http://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "prefer_cloud", "=", "True", ")", "==", "\"http://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"http://example.com\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_external_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "# Test with external url: https://example.com:443/", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"external_url\"", ":", "\"https://example.com:443/\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "external_url", "==", "\"https://example.com:443/\"", "assert", "_get_external_url", "(", "hass", ")", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "allow_cloud", "=", "False", ")", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "prefer_cloud", "=", "True", ")", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "require_ssl", "=", "False", ")", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://example.com\"", "# Test with external URL: https://example.com:80", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"external_url\"", ":", "\"https://example.com:80\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "external_url", "==", "\"https://example.com:80\"", "assert", "_get_external_url", "(", "hass", ")", "==", "\"https://example.com:80\"", "assert", "_get_external_url", "(", "hass", ",", "allow_cloud", "=", "False", ")", "==", "\"https://example.com:80\"", "assert", "_get_external_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"https://example.com:80\"", "assert", "_get_external_url", "(", "hass", ",", "prefer_cloud", "=", "True", ")", "==", "\"https://example.com:80\"", "assert", "_get_external_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://example.com:80\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_external_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "# Test with external URL: https://192.168.0.1", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"external_url\"", ":", "\"https://192.168.0.1\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "external_url", "==", "\"https://192.168.0.1\"", "assert", "_get_external_url", "(", "hass", ")", "==", "\"https://192.168.0.1\"", "assert", "_get_external_url", "(", "hass", ",", "allow_cloud", "=", "False", ")", "==", "\"https://192.168.0.1\"", "assert", "_get_external_url", "(", "hass", ",", "prefer_cloud", "=", "True", ")", "==", "\"https://192.168.0.1\"", "assert", "_get_external_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://192.168.0.1\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_external_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_external_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"192.168.0.1\"", ")", ":", "assert", "(", "_get_external_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "==", "\"https://192.168.0.1\"", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_external_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "allow_ip", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_external_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "require_ssl", "=", "True", ")" ]
[ 224, 0 ]
[ 347, 83 ]
python
en
['en', 'lb', 'en']
True
test_get_cloud_url
(hass: HomeAssistant)
Test getting an instance URL when the user has set an external URL.
Test getting an instance URL when the user has set an external URL.
async def test_get_cloud_url(hass: HomeAssistant): """Test getting an instance URL when the user has set an external URL.""" assert hass.config.external_url is None hass.config.components.add("cloud") with patch.object( hass.components.cloud, "async_remote_ui_url", return_value="https://example.nabu.casa", ): assert _get_cloud_url(hass) == "https://example.nabu.casa" with pytest.raises(NoURLAvailableError): _get_cloud_url(hass, require_current_request=True) with patch( "homeassistant.helpers.network._get_request_host", return_value="example.nabu.casa", ): assert ( _get_cloud_url(hass, require_current_request=True) == "https://example.nabu.casa" ) with patch( "homeassistant.helpers.network._get_request_host", return_value="no_match.nabu.casa", ), pytest.raises(NoURLAvailableError): _get_cloud_url(hass, require_current_request=True) with patch.object( hass.components.cloud, "async_remote_ui_url", side_effect=cloud.CloudNotAvailable, ): with pytest.raises(NoURLAvailableError): _get_cloud_url(hass)
[ "async", "def", "test_get_cloud_url", "(", "hass", ":", "HomeAssistant", ")", ":", "assert", "hass", ".", "config", ".", "external_url", "is", "None", "hass", ".", "config", ".", "components", ".", "add", "(", "\"cloud\"", ")", "with", "patch", ".", "object", "(", "hass", ".", "components", ".", "cloud", ",", "\"async_remote_ui_url\"", ",", "return_value", "=", "\"https://example.nabu.casa\"", ",", ")", ":", "assert", "_get_cloud_url", "(", "hass", ")", "==", "\"https://example.nabu.casa\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_cloud_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"example.nabu.casa\"", ",", ")", ":", "assert", "(", "_get_cloud_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "==", "\"https://example.nabu.casa\"", ")", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"no_match.nabu.casa\"", ",", ")", ",", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_cloud_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "with", "patch", ".", "object", "(", "hass", ".", "components", ".", "cloud", ",", "\"async_remote_ui_url\"", ",", "side_effect", "=", "cloud", ".", "CloudNotAvailable", ",", ")", ":", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_cloud_url", "(", "hass", ")" ]
[ 350, 0 ]
[ 386, 32 ]
python
en
['en', 'lb', 'en']
True
test_get_external_url_cloud_fallback
(hass: HomeAssistant)
Test getting an external instance URL with cloud fallback.
Test getting an external instance URL with cloud fallback.
async def test_get_external_url_cloud_fallback(hass: HomeAssistant): """Test getting an external instance URL with cloud fallback.""" assert hass.config.external_url is None # Test with external URL: http://1.1.1.1:8123 await async_process_ha_core_config( hass, {"external_url": "http://1.1.1.1:8123"}, ) assert hass.config.external_url == "http://1.1.1.1:8123" assert _get_external_url(hass, prefer_cloud=True) == "http://1.1.1.1:8123" # Add Cloud to the previous test hass.config.components.add("cloud") with patch.object( hass.components.cloud, "async_remote_ui_url", return_value="https://example.nabu.casa", ): assert _get_external_url(hass, allow_cloud=False) == "http://1.1.1.1:8123" assert _get_external_url(hass, allow_ip=False) == "https://example.nabu.casa" assert _get_external_url(hass, prefer_cloud=False) == "http://1.1.1.1:8123" assert _get_external_url(hass, prefer_cloud=True) == "https://example.nabu.casa" assert _get_external_url(hass, require_ssl=True) == "https://example.nabu.casa" assert ( _get_external_url(hass, require_standard_port=True) == "https://example.nabu.casa" ) # Test with external URL: https://example.com await async_process_ha_core_config( hass, {"external_url": "https://example.com"}, ) assert hass.config.external_url == "https://example.com" assert _get_external_url(hass, prefer_cloud=True) == "https://example.com" # Add Cloud to the previous test hass.config.components.add("cloud") with patch.object( hass.components.cloud, "async_remote_ui_url", return_value="https://example.nabu.casa", ): assert _get_external_url(hass, allow_cloud=False) == "https://example.com" assert _get_external_url(hass, allow_ip=False) == "https://example.com" assert _get_external_url(hass, prefer_cloud=False) == "https://example.com" assert _get_external_url(hass, prefer_cloud=True) == "https://example.nabu.casa" assert _get_external_url(hass, require_ssl=True) == "https://example.com" assert ( _get_external_url(hass, require_standard_port=True) == "https://example.com" ) assert ( _get_external_url(hass, prefer_cloud=True, allow_cloud=False) == "https://example.com" )
[ "async", "def", "test_get_external_url_cloud_fallback", "(", "hass", ":", "HomeAssistant", ")", ":", "assert", "hass", ".", "config", ".", "external_url", "is", "None", "# Test with external URL: http://1.1.1.1:8123", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"external_url\"", ":", "\"http://1.1.1.1:8123\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "external_url", "==", "\"http://1.1.1.1:8123\"", "assert", "_get_external_url", "(", "hass", ",", "prefer_cloud", "=", "True", ")", "==", "\"http://1.1.1.1:8123\"", "# Add Cloud to the previous test", "hass", ".", "config", ".", "components", ".", "add", "(", "\"cloud\"", ")", "with", "patch", ".", "object", "(", "hass", ".", "components", ".", "cloud", ",", "\"async_remote_ui_url\"", ",", "return_value", "=", "\"https://example.nabu.casa\"", ",", ")", ":", "assert", "_get_external_url", "(", "hass", ",", "allow_cloud", "=", "False", ")", "==", "\"http://1.1.1.1:8123\"", "assert", "_get_external_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"https://example.nabu.casa\"", "assert", "_get_external_url", "(", "hass", ",", "prefer_cloud", "=", "False", ")", "==", "\"http://1.1.1.1:8123\"", "assert", "_get_external_url", "(", "hass", ",", "prefer_cloud", "=", "True", ")", "==", "\"https://example.nabu.casa\"", "assert", "_get_external_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://example.nabu.casa\"", "assert", "(", "_get_external_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://example.nabu.casa\"", ")", "# Test with external URL: https://example.com", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"external_url\"", ":", "\"https://example.com\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "external_url", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "prefer_cloud", "=", "True", ")", "==", "\"https://example.com\"", "# Add Cloud to the previous test", "hass", ".", "config", ".", "components", ".", "add", "(", "\"cloud\"", ")", "with", "patch", ".", "object", "(", "hass", ".", "components", ".", "cloud", ",", "\"async_remote_ui_url\"", ",", "return_value", "=", "\"https://example.nabu.casa\"", ",", ")", ":", "assert", "_get_external_url", "(", "hass", ",", "allow_cloud", "=", "False", ")", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "prefer_cloud", "=", "False", ")", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "prefer_cloud", "=", "True", ")", "==", "\"https://example.nabu.casa\"", "assert", "_get_external_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://example.com\"", "assert", "(", "_get_external_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://example.com\"", ")", "assert", "(", "_get_external_url", "(", "hass", ",", "prefer_cloud", "=", "True", ",", "allow_cloud", "=", "False", ")", "==", "\"https://example.com\"", ")" ]
[ 389, 0 ]
[ 446, 9 ]
python
en
['en', 'en', 'en']
True
test_get_url
(hass: HomeAssistant)
Test getting an instance URL.
Test getting an instance URL.
async def test_get_url(hass: HomeAssistant): """Test getting an instance URL.""" assert hass.config.external_url is None assert hass.config.internal_url is None with pytest.raises(NoURLAvailableError): get_url(hass) hass.config.api = Mock( use_ssl=False, port=8123, deprecated_base_url=None, local_ip="192.168.123.123" ) assert get_url(hass) == "http://192.168.123.123:8123" assert get_url(hass, prefer_external=True) == "http://192.168.123.123:8123" with pytest.raises(NoURLAvailableError): get_url(hass, allow_internal=False) # Test only external hass.config.api = None await async_process_ha_core_config( hass, {"external_url": "https://example.com"}, ) assert hass.config.external_url == "https://example.com" assert hass.config.internal_url is None assert get_url(hass) == "https://example.com" # Test preference or allowance await async_process_ha_core_config( hass, {"internal_url": "http://example.local", "external_url": "https://example.com"}, ) assert hass.config.external_url == "https://example.com" assert hass.config.internal_url == "http://example.local" assert get_url(hass) == "http://example.local" assert get_url(hass, prefer_external=True) == "https://example.com" assert get_url(hass, allow_internal=False) == "https://example.com" assert ( get_url(hass, prefer_external=True, allow_external=False) == "http://example.local" ) with pytest.raises(NoURLAvailableError): get_url(hass, allow_external=False, require_ssl=True) with pytest.raises(NoURLAvailableError): get_url(hass, allow_external=False, allow_internal=False) with pytest.raises(NoURLAvailableError): get_url(hass, require_current_request=True) with patch( "homeassistant.helpers.network._get_request_host", return_value="example.com" ), patch("homeassistant.helpers.network.current_request"): assert get_url(hass, require_current_request=True) == "https://example.com" assert ( get_url(hass, require_current_request=True, require_ssl=True) == "https://example.com" ) with pytest.raises(NoURLAvailableError): get_url(hass, require_current_request=True, allow_external=False) with patch( "homeassistant.helpers.network._get_request_host", return_value="example.local" ), patch("homeassistant.helpers.network.current_request"): assert get_url(hass, require_current_request=True) == "http://example.local" with pytest.raises(NoURLAvailableError): get_url(hass, require_current_request=True, allow_internal=False) with pytest.raises(NoURLAvailableError): get_url(hass, require_current_request=True, require_ssl=True) with patch( "homeassistant.helpers.network._get_request_host", return_value="no_match.example.com", ), pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_current_request=True)
[ "async", "def", "test_get_url", "(", "hass", ":", "HomeAssistant", ")", ":", "assert", "hass", ".", "config", ".", "external_url", "is", "None", "assert", "hass", ".", "config", ".", "internal_url", "is", "None", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "get_url", "(", "hass", ")", "hass", ".", "config", ".", "api", "=", "Mock", "(", "use_ssl", "=", "False", ",", "port", "=", "8123", ",", "deprecated_base_url", "=", "None", ",", "local_ip", "=", "\"192.168.123.123\"", ")", "assert", "get_url", "(", "hass", ")", "==", "\"http://192.168.123.123:8123\"", "assert", "get_url", "(", "hass", ",", "prefer_external", "=", "True", ")", "==", "\"http://192.168.123.123:8123\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "get_url", "(", "hass", ",", "allow_internal", "=", "False", ")", "# Test only external", "hass", ".", "config", ".", "api", "=", "None", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"external_url\"", ":", "\"https://example.com\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "external_url", "==", "\"https://example.com\"", "assert", "hass", ".", "config", ".", "internal_url", "is", "None", "assert", "get_url", "(", "hass", ")", "==", "\"https://example.com\"", "# Test preference or allowance", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"internal_url\"", ":", "\"http://example.local\"", ",", "\"external_url\"", ":", "\"https://example.com\"", "}", ",", ")", "assert", "hass", ".", "config", ".", "external_url", "==", "\"https://example.com\"", "assert", "hass", ".", "config", ".", "internal_url", "==", "\"http://example.local\"", "assert", "get_url", "(", "hass", ")", "==", "\"http://example.local\"", "assert", "get_url", "(", "hass", ",", "prefer_external", "=", "True", ")", "==", "\"https://example.com\"", "assert", "get_url", "(", "hass", ",", "allow_internal", "=", "False", ")", "==", "\"https://example.com\"", "assert", "(", "get_url", "(", "hass", ",", "prefer_external", "=", "True", ",", "allow_external", "=", "False", ")", "==", "\"http://example.local\"", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "get_url", "(", "hass", ",", "allow_external", "=", "False", ",", "require_ssl", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "get_url", "(", "hass", ",", "allow_external", "=", "False", ",", "allow_internal", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"example.com\"", ")", ",", "patch", "(", "\"homeassistant.helpers.network.current_request\"", ")", ":", "assert", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "==", "\"https://example.com\"", "assert", "(", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "require_ssl", "=", "True", ")", "==", "\"https://example.com\"", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "allow_external", "=", "False", ")", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"example.local\"", ")", ",", "patch", "(", "\"homeassistant.helpers.network.current_request\"", ")", ":", "assert", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "==", "\"http://example.local\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "allow_internal", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "require_ssl", "=", "True", ")", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"no_match.example.com\"", ",", ")", ",", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_current_request", "=", "True", ")" ]
[ 449, 0 ]
[ 527, 61 ]
python
en
['en', 'lb', 'en']
True
test_get_request_host
(hass: HomeAssistant)
Test getting the host of the current web request from the request context.
Test getting the host of the current web request from the request context.
async def test_get_request_host(hass: HomeAssistant): """Test getting the host of the current web request from the request context.""" with pytest.raises(NoURLAvailableError): _get_request_host() with patch("homeassistant.helpers.network.current_request") as mock_request_context: mock_request = Mock() mock_request.url = "http://example.com:8123/test/request" mock_request_context.get = Mock(return_value=mock_request) assert _get_request_host() == "example.com"
[ "async", "def", "test_get_request_host", "(", "hass", ":", "HomeAssistant", ")", ":", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_request_host", "(", ")", "with", "patch", "(", "\"homeassistant.helpers.network.current_request\"", ")", "as", "mock_request_context", ":", "mock_request", "=", "Mock", "(", ")", "mock_request", ".", "url", "=", "\"http://example.com:8123/test/request\"", "mock_request_context", ".", "get", "=", "Mock", "(", "return_value", "=", "mock_request", ")", "assert", "_get_request_host", "(", ")", "==", "\"example.com\"" ]
[ 530, 0 ]
[ 540, 51 ]
python
en
['en', 'en', 'en']
True
test_get_deprecated_base_url_internal
(hass: HomeAssistant)
Test getting an internal instance URL from the deprecated base_url.
Test getting an internal instance URL from the deprecated base_url.
async def test_get_deprecated_base_url_internal(hass: HomeAssistant): """Test getting an internal instance URL from the deprecated base_url.""" # Test with SSL local URL hass.config.api = Mock(deprecated_base_url="https://example.local") assert _get_deprecated_base_url(hass, internal=True) == "https://example.local" assert ( _get_deprecated_base_url(hass, internal=True, allow_ip=False) == "https://example.local" ) assert ( _get_deprecated_base_url(hass, internal=True, require_ssl=True) == "https://example.local" ) assert ( _get_deprecated_base_url(hass, internal=True, require_standard_port=True) == "https://example.local" ) # Test with no SSL, local IP URL hass.config.api = Mock(deprecated_base_url="http://10.10.10.10:8123") assert _get_deprecated_base_url(hass, internal=True) == "http://10.10.10.10:8123" with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, internal=True, allow_ip=False) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, internal=True, require_ssl=True) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, internal=True, require_standard_port=True) # Test with SSL, local IP URL hass.config.api = Mock(deprecated_base_url="https://10.10.10.10") assert _get_deprecated_base_url(hass, internal=True) == "https://10.10.10.10" assert ( _get_deprecated_base_url(hass, internal=True, require_ssl=True) == "https://10.10.10.10" ) assert ( _get_deprecated_base_url(hass, internal=True, require_standard_port=True) == "https://10.10.10.10" ) # Test external URL hass.config.api = Mock(deprecated_base_url="https://example.com") with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, internal=True) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, internal=True, require_ssl=True) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, internal=True, require_standard_port=True) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, internal=True, allow_ip=False) # Test with loopback hass.config.api = Mock(deprecated_base_url="https://127.0.0.42") with pytest.raises(NoURLAvailableError): assert _get_deprecated_base_url(hass, internal=True) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, internal=True, allow_ip=False) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, internal=True, require_ssl=True) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, internal=True, require_standard_port=True)
[ "async", "def", "test_get_deprecated_base_url_internal", "(", "hass", ":", "HomeAssistant", ")", ":", "# Test with SSL local URL", "hass", ".", "config", ".", "api", "=", "Mock", "(", "deprecated_base_url", "=", "\"https://example.local\"", ")", "assert", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ")", "==", "\"https://example.local\"", "assert", "(", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ",", "allow_ip", "=", "False", ")", "==", "\"https://example.local\"", ")", "assert", "(", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ",", "require_ssl", "=", "True", ")", "==", "\"https://example.local\"", ")", "assert", "(", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ",", "require_standard_port", "=", "True", ")", "==", "\"https://example.local\"", ")", "# Test with no SSL, local IP URL", "hass", ".", "config", ".", "api", "=", "Mock", "(", "deprecated_base_url", "=", "\"http://10.10.10.10:8123\"", ")", "assert", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ")", "==", "\"http://10.10.10.10:8123\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ",", "allow_ip", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ",", "require_ssl", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ",", "require_standard_port", "=", "True", ")", "# Test with SSL, local IP URL", "hass", ".", "config", ".", "api", "=", "Mock", "(", "deprecated_base_url", "=", "\"https://10.10.10.10\"", ")", "assert", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ")", "==", "\"https://10.10.10.10\"", "assert", "(", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ",", "require_ssl", "=", "True", ")", "==", "\"https://10.10.10.10\"", ")", "assert", "(", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ",", "require_standard_port", "=", "True", ")", "==", "\"https://10.10.10.10\"", ")", "# Test external URL", "hass", ".", "config", ".", "api", "=", "Mock", "(", "deprecated_base_url", "=", "\"https://example.com\"", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ",", "require_ssl", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ",", "require_standard_port", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ",", "allow_ip", "=", "False", ")", "# Test with loopback", "hass", ".", "config", ".", "api", "=", "Mock", "(", "deprecated_base_url", "=", "\"https://127.0.0.42\"", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "assert", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ",", "allow_ip", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ",", "require_ssl", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "internal", "=", "True", ",", "require_standard_port", "=", "True", ")" ]
[ 543, 0 ]
[ 612, 81 ]
python
en
['en', 'en', 'en']
True
test_get_deprecated_base_url_external
(hass: HomeAssistant)
Test getting an external instance URL from the deprecated base_url.
Test getting an external instance URL from the deprecated base_url.
async def test_get_deprecated_base_url_external(hass: HomeAssistant): """Test getting an external instance URL from the deprecated base_url.""" # Test with SSL and external domain on standard port hass.config.api = Mock(deprecated_base_url="https://example.com:443/") assert _get_deprecated_base_url(hass) == "https://example.com" assert _get_deprecated_base_url(hass, require_ssl=True) == "https://example.com" assert ( _get_deprecated_base_url(hass, require_standard_port=True) == "https://example.com" ) # Test without SSL and external domain on non-standard port hass.config.api = Mock(deprecated_base_url="http://example.com:8123/") assert _get_deprecated_base_url(hass) == "http://example.com:8123" with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, require_ssl=True) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, require_standard_port=True) # Test SSL on external IP hass.config.api = Mock(deprecated_base_url="https://1.1.1.1") assert _get_deprecated_base_url(hass) == "https://1.1.1.1" assert _get_deprecated_base_url(hass, require_ssl=True) == "https://1.1.1.1" assert ( _get_deprecated_base_url(hass, require_standard_port=True) == "https://1.1.1.1" ) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, allow_ip=False) # Test with private IP hass.config.api = Mock(deprecated_base_url="https://10.10.10.10") with pytest.raises(NoURLAvailableError): assert _get_deprecated_base_url(hass) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, allow_ip=False) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, require_ssl=True) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, require_standard_port=True) # Test with local domain hass.config.api = Mock(deprecated_base_url="https://example.local") with pytest.raises(NoURLAvailableError): assert _get_deprecated_base_url(hass) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, allow_ip=False) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, require_ssl=True) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, require_standard_port=True) # Test with loopback hass.config.api = Mock(deprecated_base_url="https://127.0.0.42") with pytest.raises(NoURLAvailableError): assert _get_deprecated_base_url(hass) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, allow_ip=False) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, require_ssl=True) with pytest.raises(NoURLAvailableError): _get_deprecated_base_url(hass, require_standard_port=True)
[ "async", "def", "test_get_deprecated_base_url_external", "(", "hass", ":", "HomeAssistant", ")", ":", "# Test with SSL and external domain on standard port", "hass", ".", "config", ".", "api", "=", "Mock", "(", "deprecated_base_url", "=", "\"https://example.com:443/\"", ")", "assert", "_get_deprecated_base_url", "(", "hass", ")", "==", "\"https://example.com\"", "assert", "_get_deprecated_base_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://example.com\"", "assert", "(", "_get_deprecated_base_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://example.com\"", ")", "# Test without SSL and external domain on non-standard port", "hass", ".", "config", ".", "api", "=", "Mock", "(", "deprecated_base_url", "=", "\"http://example.com:8123/\"", ")", "assert", "_get_deprecated_base_url", "(", "hass", ")", "==", "\"http://example.com:8123\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "# Test SSL on external IP", "hass", ".", "config", ".", "api", "=", "Mock", "(", "deprecated_base_url", "=", "\"https://1.1.1.1\"", ")", "assert", "_get_deprecated_base_url", "(", "hass", ")", "==", "\"https://1.1.1.1\"", "assert", "_get_deprecated_base_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://1.1.1.1\"", "assert", "(", "_get_deprecated_base_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://1.1.1.1\"", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "# Test with private IP", "hass", ".", "config", ".", "api", "=", "Mock", "(", "deprecated_base_url", "=", "\"https://10.10.10.10\"", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "assert", "_get_deprecated_base_url", "(", "hass", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "# Test with local domain", "hass", ".", "config", ".", "api", "=", "Mock", "(", "deprecated_base_url", "=", "\"https://example.local\"", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "assert", "_get_deprecated_base_url", "(", "hass", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "# Test with loopback", "hass", ".", "config", ".", "api", "=", "Mock", "(", "deprecated_base_url", "=", "\"https://127.0.0.42\"", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "assert", "_get_deprecated_base_url", "(", "hass", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_deprecated_base_url", "(", "hass", ",", "require_standard_port", "=", "True", ")" ]
[ 615, 0 ]
[ 687, 66 ]
python
en
['en', 'en', 'en']
True
test_get_internal_url_with_base_url_fallback
(hass: HomeAssistant)
Test getting an internal instance URL with the deprecated base_url fallback.
Test getting an internal instance URL with the deprecated base_url fallback.
async def test_get_internal_url_with_base_url_fallback(hass: HomeAssistant): """Test getting an internal instance URL with the deprecated base_url fallback.""" hass.config.api = Mock( use_ssl=False, port=8123, deprecated_base_url=None, local_ip="192.168.123.123" ) assert hass.config.internal_url is None assert _get_internal_url(hass) == "http://192.168.123.123:8123" with pytest.raises(NoURLAvailableError): _get_internal_url(hass, allow_ip=False) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_standard_port=True) with pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_ssl=True) # Add base_url hass.config.api = Mock( use_ssl=False, port=8123, deprecated_base_url="https://example.local" ) assert _get_internal_url(hass) == "https://example.local" assert _get_internal_url(hass, allow_ip=False) == "https://example.local" assert ( _get_internal_url(hass, require_standard_port=True) == "https://example.local" ) assert _get_internal_url(hass, require_ssl=True) == "https://example.local" # Add internal URL await async_process_ha_core_config( hass, {"internal_url": "https://internal.local"}, ) assert _get_internal_url(hass) == "https://internal.local" assert _get_internal_url(hass, allow_ip=False) == "https://internal.local" assert ( _get_internal_url(hass, require_standard_port=True) == "https://internal.local" ) assert _get_internal_url(hass, require_ssl=True) == "https://internal.local" # Add internal URL, mixed results await async_process_ha_core_config( hass, {"internal_url": "http://internal.local:8123"}, ) assert _get_internal_url(hass) == "http://internal.local:8123" assert _get_internal_url(hass, allow_ip=False) == "http://internal.local:8123" assert ( _get_internal_url(hass, require_standard_port=True) == "https://example.local" ) assert _get_internal_url(hass, require_ssl=True) == "https://example.local" # Add internal URL set to an IP await async_process_ha_core_config( hass, {"internal_url": "http://10.10.10.10:8123"}, ) assert _get_internal_url(hass) == "http://10.10.10.10:8123" assert _get_internal_url(hass, allow_ip=False) == "https://example.local" assert ( _get_internal_url(hass, require_standard_port=True) == "https://example.local" ) assert _get_internal_url(hass, require_ssl=True) == "https://example.local"
[ "async", "def", "test_get_internal_url_with_base_url_fallback", "(", "hass", ":", "HomeAssistant", ")", ":", "hass", ".", "config", ".", "api", "=", "Mock", "(", "use_ssl", "=", "False", ",", "port", "=", "8123", ",", "deprecated_base_url", "=", "None", ",", "local_ip", "=", "\"192.168.123.123\"", ")", "assert", "hass", ".", "config", ".", "internal_url", "is", "None", "assert", "_get_internal_url", "(", "hass", ")", "==", "\"http://192.168.123.123:8123\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "# Add base_url", "hass", ".", "config", ".", "api", "=", "Mock", "(", "use_ssl", "=", "False", ",", "port", "=", "8123", ",", "deprecated_base_url", "=", "\"https://example.local\"", ")", "assert", "_get_internal_url", "(", "hass", ")", "==", "\"https://example.local\"", "assert", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"https://example.local\"", "assert", "(", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://example.local\"", ")", "assert", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://example.local\"", "# Add internal URL", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"internal_url\"", ":", "\"https://internal.local\"", "}", ",", ")", "assert", "_get_internal_url", "(", "hass", ")", "==", "\"https://internal.local\"", "assert", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"https://internal.local\"", "assert", "(", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://internal.local\"", ")", "assert", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://internal.local\"", "# Add internal URL, mixed results", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"internal_url\"", ":", "\"http://internal.local:8123\"", "}", ",", ")", "assert", "_get_internal_url", "(", "hass", ")", "==", "\"http://internal.local:8123\"", "assert", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"http://internal.local:8123\"", "assert", "(", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://example.local\"", ")", "assert", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://example.local\"", "# Add internal URL set to an IP", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"internal_url\"", ":", "\"http://10.10.10.10:8123\"", "}", ",", ")", "assert", "_get_internal_url", "(", "hass", ")", "==", "\"http://10.10.10.10:8123\"", "assert", "_get_internal_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"https://example.local\"", "assert", "(", "_get_internal_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://example.local\"", ")", "assert", "_get_internal_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://example.local\"" ]
[ 690, 0 ]
[ 752, 79 ]
python
en
['en', 'en', 'en']
True
test_get_external_url_with_base_url_fallback
(hass: HomeAssistant)
Test getting an external instance URL with the deprecated base_url fallback.
Test getting an external instance URL with the deprecated base_url fallback.
async def test_get_external_url_with_base_url_fallback(hass: HomeAssistant): """Test getting an external instance URL with the deprecated base_url fallback.""" hass.config.api = Mock(use_ssl=False, port=8123, deprecated_base_url=None) assert hass.config.internal_url is None with pytest.raises(NoURLAvailableError): _get_external_url(hass) # Test with SSL and external domain on standard port hass.config.api = Mock(deprecated_base_url="https://example.com:443/") assert _get_external_url(hass) == "https://example.com" assert _get_external_url(hass, allow_ip=False) == "https://example.com" assert _get_external_url(hass, require_ssl=True) == "https://example.com" assert _get_external_url(hass, require_standard_port=True) == "https://example.com" # Add external URL await async_process_ha_core_config( hass, {"external_url": "https://external.example.com"}, ) assert _get_external_url(hass) == "https://external.example.com" assert _get_external_url(hass, allow_ip=False) == "https://external.example.com" assert ( _get_external_url(hass, require_standard_port=True) == "https://external.example.com" ) assert _get_external_url(hass, require_ssl=True) == "https://external.example.com" # Add external URL, mixed results await async_process_ha_core_config( hass, {"external_url": "http://external.example.com:8123"}, ) assert _get_external_url(hass) == "http://external.example.com:8123" assert _get_external_url(hass, allow_ip=False) == "http://external.example.com:8123" assert _get_external_url(hass, require_standard_port=True) == "https://example.com" assert _get_external_url(hass, require_ssl=True) == "https://example.com" # Add external URL set to an IP await async_process_ha_core_config( hass, {"external_url": "http://1.1.1.1:8123"}, ) assert _get_external_url(hass) == "http://1.1.1.1:8123" assert _get_external_url(hass, allow_ip=False) == "https://example.com" assert _get_external_url(hass, require_standard_port=True) == "https://example.com" assert _get_external_url(hass, require_ssl=True) == "https://example.com"
[ "async", "def", "test_get_external_url_with_base_url_fallback", "(", "hass", ":", "HomeAssistant", ")", ":", "hass", ".", "config", ".", "api", "=", "Mock", "(", "use_ssl", "=", "False", ",", "port", "=", "8123", ",", "deprecated_base_url", "=", "None", ")", "assert", "hass", ".", "config", ".", "internal_url", "is", "None", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "_get_external_url", "(", "hass", ")", "# Test with SSL and external domain on standard port", "hass", ".", "config", ".", "api", "=", "Mock", "(", "deprecated_base_url", "=", "\"https://example.com:443/\"", ")", "assert", "_get_external_url", "(", "hass", ")", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://example.com\"", "# Add external URL", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"external_url\"", ":", "\"https://external.example.com\"", "}", ",", ")", "assert", "_get_external_url", "(", "hass", ")", "==", "\"https://external.example.com\"", "assert", "_get_external_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"https://external.example.com\"", "assert", "(", "_get_external_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://external.example.com\"", ")", "assert", "_get_external_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://external.example.com\"", "# Add external URL, mixed results", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"external_url\"", ":", "\"http://external.example.com:8123\"", "}", ",", ")", "assert", "_get_external_url", "(", "hass", ")", "==", "\"http://external.example.com:8123\"", "assert", "_get_external_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"http://external.example.com:8123\"", "assert", "_get_external_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://example.com\"", "# Add external URL set to an IP", "await", "async_process_ha_core_config", "(", "hass", ",", "{", "\"external_url\"", ":", "\"http://1.1.1.1:8123\"", "}", ",", ")", "assert", "_get_external_url", "(", "hass", ")", "==", "\"http://1.1.1.1:8123\"", "assert", "_get_external_url", "(", "hass", ",", "allow_ip", "=", "False", ")", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "require_standard_port", "=", "True", ")", "==", "\"https://example.com\"", "assert", "_get_external_url", "(", "hass", ",", "require_ssl", "=", "True", ")", "==", "\"https://example.com\"" ]
[ 755, 0 ]
[ 801, 77 ]
python
en
['en', 'en', 'en']
True
test_get_current_request_url_with_known_host
( hass: HomeAssistant, current_request )
Test getting current request URL with known hosts addresses.
Test getting current request URL with known hosts addresses.
async def test_get_current_request_url_with_known_host( hass: HomeAssistant, current_request ): """Test getting current request URL with known hosts addresses.""" hass.config.api = Mock( use_ssl=False, port=8123, local_ip="127.0.0.1", deprecated_base_url=None ) assert hass.config.internal_url is None with pytest.raises(NoURLAvailableError): get_url(hass, require_current_request=True) # Ensure we accept localhost with patch( "homeassistant.helpers.network._get_request_host", return_value="localhost" ): assert get_url(hass, require_current_request=True) == "http://localhost:8123" with pytest.raises(NoURLAvailableError): get_url(hass, require_current_request=True, require_ssl=True) with pytest.raises(NoURLAvailableError): get_url(hass, require_current_request=True, require_standard_port=True) # Ensure we accept local loopback ip (e.g., 127.0.0.1) with patch( "homeassistant.helpers.network._get_request_host", return_value="127.0.0.8" ): assert get_url(hass, require_current_request=True) == "http://127.0.0.8:8123" with pytest.raises(NoURLAvailableError): get_url(hass, require_current_request=True, allow_ip=False) # Ensure hostname from Supervisor is accepted transparently mock_component(hass, "hassio") hass.components.hassio.is_hassio = Mock(return_value=True) hass.components.hassio.get_host_info = Mock( return_value={"hostname": "homeassistant"} ) with patch( "homeassistant.helpers.network._get_request_host", return_value="homeassistant.local", ): assert ( get_url(hass, require_current_request=True) == "http://homeassistant.local:8123" ) with patch( "homeassistant.helpers.network._get_request_host", return_value="homeassistant", ): assert ( get_url(hass, require_current_request=True) == "http://homeassistant:8123" ) with patch( "homeassistant.helpers.network._get_request_host", return_value="unknown.local" ), pytest.raises(NoURLAvailableError): get_url(hass, require_current_request=True)
[ "async", "def", "test_get_current_request_url_with_known_host", "(", "hass", ":", "HomeAssistant", ",", "current_request", ")", ":", "hass", ".", "config", ".", "api", "=", "Mock", "(", "use_ssl", "=", "False", ",", "port", "=", "8123", ",", "local_ip", "=", "\"127.0.0.1\"", ",", "deprecated_base_url", "=", "None", ")", "assert", "hass", ".", "config", ".", "internal_url", "is", "None", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "# Ensure we accept localhost", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"localhost\"", ")", ":", "assert", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "==", "\"http://localhost:8123\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "require_ssl", "=", "True", ")", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "require_standard_port", "=", "True", ")", "# Ensure we accept local loopback ip (e.g., 127.0.0.1)", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"127.0.0.8\"", ")", ":", "assert", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "==", "\"http://127.0.0.8:8123\"", "with", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ",", "allow_ip", "=", "False", ")", "# Ensure hostname from Supervisor is accepted transparently", "mock_component", "(", "hass", ",", "\"hassio\"", ")", "hass", ".", "components", ".", "hassio", ".", "is_hassio", "=", "Mock", "(", "return_value", "=", "True", ")", "hass", ".", "components", ".", "hassio", ".", "get_host_info", "=", "Mock", "(", "return_value", "=", "{", "\"hostname\"", ":", "\"homeassistant\"", "}", ")", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"homeassistant.local\"", ",", ")", ":", "assert", "(", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "==", "\"http://homeassistant.local:8123\"", ")", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"homeassistant\"", ",", ")", ":", "assert", "(", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ")", "==", "\"http://homeassistant:8123\"", ")", "with", "patch", "(", "\"homeassistant.helpers.network._get_request_host\"", ",", "return_value", "=", "\"unknown.local\"", ")", ",", "pytest", ".", "raises", "(", "NoURLAvailableError", ")", ":", "get_url", "(", "hass", ",", "require_current_request", "=", "True", ")" ]
[ 804, 0 ]
[ 861, 51 ]
python
en
['en', 'en', 'en']
True
calculate_next_active_alarms
(alarms)
Calculate garmin next active alarms from settings. Alarms are sorted by time
Calculate garmin next active alarms from settings.
def calculate_next_active_alarms(alarms): """ Calculate garmin next active alarms from settings. Alarms are sorted by time """ active_alarms = [] _LOGGER.debug(alarms) for alarm_setting in alarms: if alarm_setting["alarmMode"] != "ON": continue for day in alarm_setting["alarmDays"]: alarm_time = alarm_setting["alarmTime"] if day == "ONCE": midnight = datetime.combine(date.today(), datetime.min.time()) alarm = midnight + timedelta(minutes=alarm_time) if alarm < datetime.now(): alarm += timedelta(days=1) else: start_of_week = datetime.combine( date.today() - timedelta(days=datetime.today().isoweekday() % 7), datetime.min.time(), ) days_to_add = DAY_TO_NUMBER[day] % 7 alarm = start_of_week + timedelta(minutes=alarm_time, days=days_to_add) if alarm < datetime.now(): alarm += timedelta(days=7) active_alarms.append(alarm.isoformat()) return sorted(active_alarms) if active_alarms else None
[ "def", "calculate_next_active_alarms", "(", "alarms", ")", ":", "active_alarms", "=", "[", "]", "_LOGGER", ".", "debug", "(", "alarms", ")", "for", "alarm_setting", "in", "alarms", ":", "if", "alarm_setting", "[", "\"alarmMode\"", "]", "!=", "\"ON\"", ":", "continue", "for", "day", "in", "alarm_setting", "[", "\"alarmDays\"", "]", ":", "alarm_time", "=", "alarm_setting", "[", "\"alarmTime\"", "]", "if", "day", "==", "\"ONCE\"", ":", "midnight", "=", "datetime", ".", "combine", "(", "date", ".", "today", "(", ")", ",", "datetime", ".", "min", ".", "time", "(", ")", ")", "alarm", "=", "midnight", "+", "timedelta", "(", "minutes", "=", "alarm_time", ")", "if", "alarm", "<", "datetime", ".", "now", "(", ")", ":", "alarm", "+=", "timedelta", "(", "days", "=", "1", ")", "else", ":", "start_of_week", "=", "datetime", ".", "combine", "(", "date", ".", "today", "(", ")", "-", "timedelta", "(", "days", "=", "datetime", ".", "today", "(", ")", ".", "isoweekday", "(", ")", "%", "7", ")", ",", "datetime", ".", "min", ".", "time", "(", ")", ",", ")", "days_to_add", "=", "DAY_TO_NUMBER", "[", "day", "]", "%", "7", "alarm", "=", "start_of_week", "+", "timedelta", "(", "minutes", "=", "alarm_time", ",", "days", "=", "days_to_add", ")", "if", "alarm", "<", "datetime", ".", "now", "(", ")", ":", "alarm", "+=", "timedelta", "(", "days", "=", "7", ")", "active_alarms", ".", "append", "(", "alarm", ".", "isoformat", "(", ")", ")", "return", "sorted", "(", "active_alarms", ")", "if", "active_alarms", "else", "None" ]
[ 20, 0 ]
[ 49, 59 ]
python
en
['en', 'error', 'th']
False
_include_yaml
( constructor: ExtSafeConstructor, node: ruamel.yaml.nodes.Node )
Load another YAML file and embeds it using the !include tag. Example: device_tracker: !include device_tracker.yaml
Load another YAML file and embeds it using the !include tag.
def _include_yaml( constructor: ExtSafeConstructor, node: ruamel.yaml.nodes.Node ) -> JSON_TYPE: """Load another YAML file and embeds it using the !include tag. Example: device_tracker: !include device_tracker.yaml """ if constructor.name is None: raise HomeAssistantError( "YAML include error: filename not set for %s" % node.value ) fname = os.path.join(os.path.dirname(constructor.name), node.value) return load_yaml(fname, False)
[ "def", "_include_yaml", "(", "constructor", ":", "ExtSafeConstructor", ",", "node", ":", "ruamel", ".", "yaml", ".", "nodes", ".", "Node", ")", "->", "JSON_TYPE", ":", "if", "constructor", ".", "name", "is", "None", ":", "raise", "HomeAssistantError", "(", "\"YAML include error: filename not set for %s\"", "%", "node", ".", "value", ")", "fname", "=", "os", ".", "path", ".", "join", "(", "os", ".", "path", ".", "dirname", "(", "constructor", ".", "name", ")", ",", "node", ".", "value", ")", "return", "load_yaml", "(", "fname", ",", "False", ")" ]
[ 35, 0 ]
[ 49, 34 ]
python
en
['en', 'en', 'en']
True
object_to_yaml
(data: JSON_TYPE)
Create yaml string from object.
Create yaml string from object.
def object_to_yaml(data: JSON_TYPE) -> str: """Create yaml string from object.""" yaml = YAML(typ="rt") yaml.indent(sequence=4, offset=2) stream = StringIO() try: yaml.dump(data, stream) result: str = stream.getvalue() return result except YAMLError as exc: _LOGGER.error("YAML error: %s", exc) raise HomeAssistantError(exc) from exc
[ "def", "object_to_yaml", "(", "data", ":", "JSON_TYPE", ")", "->", "str", ":", "yaml", "=", "YAML", "(", "typ", "=", "\"rt\"", ")", "yaml", ".", "indent", "(", "sequence", "=", "4", ",", "offset", "=", "2", ")", "stream", "=", "StringIO", "(", ")", "try", ":", "yaml", ".", "dump", "(", "data", ",", "stream", ")", "result", ":", "str", "=", "stream", ".", "getvalue", "(", ")", "return", "result", "except", "YAMLError", "as", "exc", ":", "_LOGGER", ".", "error", "(", "\"YAML error: %s\"", ",", "exc", ")", "raise", "HomeAssistantError", "(", "exc", ")", "from", "exc" ]
[ 61, 0 ]
[ 72, 46 ]
python
en
['en', 'en', 'en']
True