How to correctly form Homeassistant API call using python requests

The documentation on the HA website is centered around cURL, which for many purposes is more than enough.  I wanted to create a standalone python implementation of the API call to activate a script. The reason?  WAF (wife acceptance factor).  By having an easy to use desktop shortcut to activate automation, it’s much more usable.  So it goes with UI and jokes; if you have to explain it, it isn’t very good.

This was typical of my frustrations with HA implementation.  98% of HA stuff just works, 1.5% breaks with updates, and 0.5% is nebulous at best.  Pretty impressive breakdown for an open source project evolving as fast as Homeassistant is.

Calls to the API with requests are seemingly straightforward.  There is great documentation for cURL here with a cursory explanation of requests usage.

There is a phenomenal tool that translates from cURL syntax to python requests HERE.

Sadly, I discovered it much too late.

After futzing with a lot of code, including a command that turned on every “switch” in my house linked through HA, I found the following worked.

from requests import post
url = 'http://bananahammock:8123/api/services/script/turn_on?api_password=BANANAHAMMOCK'
headers = {'content-type': 'application/json'}
payload = '{"entity_id":"script.remote_shutdown_banana_hammock"}'
response = post(url, headers=headers, data=payload)
print(response.text)

The key element is to encapsulate the JSON into a string with quotes, otherwise, it will be interpreted as a dict and the API has no idea what you are presenting it with and will reject it as not being JSON and you’ll end up with this staring at you:

Clipboard01

Advertisements

AJR: Neonatal Lung Disease

05_17_19231_01

  • Most neonatal lung disorders can be diagnosed and managed with radiographs alone.
    • estimated radiation doses to neonates in the  NICU are low, ranging from 24 to 32 μ Gy
  • Diffuse Lung Disease

    • SDD
      • Decreased lung compliance leads to alveolar injury, resulting in accumulation of fibrin and cellular debris (called hyaline membranes) within the alveo li. This debris, as well as atelectasis, leads to poor oxygen exchange.
      • most children with symptomatic SDD receive respiratory support, which expands the lung volumes, and low lung volumes are not a reliable feature of SDD in modern practice
      • Radiographs may normalize or evolve to findings of chronic lung disease of prematurity (previously known as bronchopulmonary dysplasia)
      • Stages
        • I the diffuse granular opacities of SDD are seen through day 3 of life (stage I)
        • II replaced by nearly complete opacification of the lungs due to necrosis and repair of alveolar and bronchiolar epithelial cells during days 4–10 of life
        • III interstitial edema and fibrosis producing small cystlike lucencies and hyperinflation at days 10–20 of life
        • IV continued progression to fibrosis

    •  TTN
      • show diffuse pulmonary interstitial opacities
      • Small pleural effusions are common, typically manifesting as trace fluid within the fissures.
      • Lung volumes may be normal or increased.
      • The most specific imaging feature of retained fetal fluid is interval improvement within 48–72 hours. In cases where the initial radiographic appearance is atypical, consideration of birth history and follow-up radiographs are key to establishing the diagnosis.
    • MAS
      • most common cause of significant morbidity and mortality among full-term and postterm neonates
      • PTX, PIE and PMS(mediastinum) common
      • In pneumothorax and diffuse lung disease, MAS should be strongly considered.
  • Focal Neonatal Lung Disorders
  • Congenital Lobar Overinflation aka  congenital lobar emphysema
    • Neonates with congenital lobar overinflation often have chest radiographs showing retention of fetal fluid within the affected lobe, because there is delayed clearance of fluid from the affected lobe in the immediate postnatal period
    • In the first hours after birth, the fetal fluid eventually clears from the affected lobe and is replaced by air. The affected lobe then shows progressive lucency and hyperexpansion, hyperexpansion of a lobe of the lung
  • CPAM05_17_19231_table1

  • Bronchopulmonary Sequestration

    • lung tissue that is disconnected from the bronchial tree and has a systemic arterial supply
    • traditionally subdivided into intralobar and extralobar types.
      • Intralobar bronchopulmonary sequestrations share a visceral pleural covering with adjacent normal lung, and most often have venous drainage to the pulmonary vein
      • Extralobar bronchopulmonary sequestrations have their own pleural covering, and venous drainage is most often to a systemic vein
      • The nonfunctioning lung tissue within intralobar and extralobar bronchopulmonary sequestrations often contains cystic regions similar to congenital pulmonary airway malformations
      • Both types of bronchopulmonary sequestration typically occur in the lower thorax. Less common locations for extralobar broncho-pulmonary sequestration include below the diaphragm or within the mediastinum.
      • The most common appearance of bronchopulmonary sequestration on neonatal chest radiographs is a lower lobe opacity or mass, which is often indistinguishable from congenital pulmonary airway malformation
      • Intralobar bronchopulmonary sequestrations often become lucent as fetal lung fluid clears and air enters the bronchopulmonary sequestration via pores of Kohn and canals of Lambert. Extralobar bronchopulmonary sequestrations remain dense, because there is no communication with the normal lung. Ultrasound may show a systemic arterial supply from the descending thoracic or abdominal aorta, although aerated lung may obscure this finding at ultrasound. Contrast-enhanced CT angiography or MR angiography is often indicated to better define the arterial and venous supply and can be especially helpful for presurgical planning
      • Intralobar bronchopulmo-nary sequestration typically appears as a cystic lung lesion with systemic arterial supply and venous drainage to the left atrium
      • Extralobar bronchopulmonary sequestration typically appears as a solid enhancing mass with systemic arterial supply and systemic venous drainage

 

  • Diffuse or Focal Neonatal Lung Disorders

    • Neonatal Pneumonia
      • most common pathogen is group B Streptococcus species, which is transmitted during vaginal delivery from colonized mothers, although a variety of other bacteria and viruses may be the cause.
      • Pulmonary opacities can be focal but are more often diffuse and bilateral. Chest radiographs can show hazy opacities similar to those associated with SDD, coarse irregular opacities similar to those associated with MAS, or reticular opacities similar to those associated with transient tachypnea of the newborn
      • Pleural effusions are more often seen in neonatal pneumonia and can be a clue to the diagnosis. Because the clinical signs and symptoms and the radiographic findings are nonspecific, the key to diagnosis is a high index of suspicion.
      • Affected patients are often treated empirically with antibiotics. Definitive diagnosis is based on blood or respiratory culture, though cultures are often negative
  • Pulmonary Interstitial Emphysema
    • PIE is an air leak phenomenon that occurs because of rupture of the respiratory epithelium, allowing air to dissect through the pulmonary interstitium [17]. PIE is most often bilateral, although it can be unilateral or confined to one lobe. PIE most often occurs in neonates with SDD undergoing mechanical ventilation, but can occasionally be seen in neonates with other conditions, including congenital diaphragmatic hernia, MAS, and neonatal pneumonia. Other air leak phenomena may occur with PIE, including pneumothorax, pneumomediastinum, and pneumoperitoneum.
    • On chest radiographs, PIE appears as multiple small round bubbly or nonbranching linear lucencies that do not conform to the shape of air bronchograms and extend to the periphery of the lung [88, 89] (Fig. 14). Lucencies represent air within the pulmonary interstitium.
    • PIE and chronic lung disease can often be differentiated by considering the time course and reviewing prior imaging. PIE usually occurs in the first week of life as an acute change on radiographs accompanied by clinical decompensation, whereas chronic lung disease tends to produce lucencies on radiographs that slowly evolve over the first 3–4 weeks of life. Occasionally, alveoli can rapidly expand after exogenous surfactant is administered, producing an appearance that mimics that of PIE
    • This can often be differentiated from PIE by considering the clinical scenario, because patients with PIE typically experience worsening symptoms, whereas patients typically improve with surfactant-mediated alveolar expansion
    • PIE typically develops acutely during the first week of life, whereas findings of congenital lobar overinflation and congenital pulmonary airway malformation are typically present at birth
    • When one detects PIE, NICU staff should be notified immediately so that they may modify the mechanical ventilation to reduce barotrauma and mitigate further air leak